Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93743

Re: Python backreference replacing doesn't work as expected

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04; 'subject:Python': 0.05; 'method.': 0.05; 'method,': 0.07; 'subject:skip:b 10': 0.07; "'''": 0.09; 'flags,': 0.09; 'res': 0.09; 'python': 0.10; 'output': 0.13; 'argument': 0.15; 'instead.': 0.15; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'literals.': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'original:': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'res:': 0.16; 'template,': 0.16; 'wrote:': 0.16; 'string': 0.17; 'passing': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'function': 0.28; "skip:' 10": 0.28; 'be:': 0.29; 'skip:r 50': 0.29; 'code:': 0.29; "i'm": 0.30; 'code': 0.30; 'becomes': 0.30; 'received:84': 0.32; 'flags': 0.33; 'skip:b 60': 0.33; 'add': 0.34; 'could': 0.35; 'expected': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'subject:work': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'method': 0.37; 'skip:p 20': 0.38; 'end': 0.39; 'received:192': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'inline': 0.63; 'more': 0.63; 'here': 0.66; 'results': 0.66; '.replace': 0.84; 'contents,': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=OoyysHLt c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=AGkejYDSTmIA:10 a=IkcTkHD0fZMA:10 a=hbg1qZk3_XcDsD8aZkoA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
Date Sun, 12 Jul 2015 20:25:57 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Python backreference replacing doesn't work as expected
References <a88a0c98-aa8d-4b10-8162-acfb81933695@googlegroups.com>
In-Reply-To <a88a0c98-aa8d-4b10-8162-acfb81933695@googlegroups.com>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.455.1436729160.3674.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1436729160 news.xs4all.nl 2908 [2001:888:2000:d::a6]:44934
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:93743

Show key headers only | View raw


On 2015-07-12 10:40, Yonggang Chen wrote:
> There are two named groups in my pattern: myFlag and id, I want to add one more myFlag immediately before group id.
>
> Here is my current code:
> ## code begin
> # i'm using Python 3.4.2
> import re
> import os
> contents = b'''
> xdlg::xdlg(x_app* pApp, CWnd* pParent)
>      : customized_dlg((UINT)0, pParent, pApp)
>      , m_pReaderApp(pApp)
>      , m_info(pApp)
> {
>
> }
> '''
>
> pattern = rb'(?P<myFlag>[a-zA-Z0-9_]+)::(?P=myFlag).+:.+(?P<id>\(UINT\)0 *,)'
> res = re.search(pattern, contents, re.DOTALL)
> if None != res:
>      print(res.groups()) # the output is (b'xdlg', b'(UINT)0,')
>
> # 'replPattern' becomes b'(?P<myFlag>[a-zA-Z0-9_]+)::(?P=myFlag).+:.+((?P=myFlag)\\(UINT\\)0 *,)'

In a replacement template, the (?P...) parts are just literals.

> replPattern = pattern.replace(b'?P<id>', b'(?P=myFlag)', re.DOTALL)

This .replace method is a string method. It has nothing to do with
regex.

> print(replPattern)
> contents = re.sub(pattern, replPattern, contents)

You're not passing in the DOTALL flag; this function doesn't have an
argument for the flags, anyway.

You could compile the regex and then use its .sub method, or use inline
flags instead.

> print(contents)
> # code end
>
> The expected results should be:
>
> xdlg::xdlg(x_app* pApp, CWnd* pParent)
>      : customized_dlg(xdlg(UINT)0, pParent, pApp)
>      , m_pReaderApp(pApp)
>      , m_info(pApp)
> {
>
> }
>
> but now the result this the same with the original:
>
>   xdlg::xdlg(x_app* pApp, CWnd* pParent)
>      : customized_dlg((UINT)0, pParent, pApp)
>      , m_pReaderApp(pApp)
>      , m_info(pApp)
> {
>
> }
>

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Python backreference replacing doesn't work as expected Yonggang Chen <yonggangchen168@gmail.com> - 2015-07-12 02:40 -0700
  Re: Python backreference replacing doesn't work as expected MRAB <python@mrabarnett.plus.com> - 2015-07-12 20:25 +0100

csiph-web