Python backreference replacing doesn't work as expected

Yonggang Chen yonggangchen168 at gmail.com
Sun Jul 12 05:40:36 EDT 2015


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 *,)'
replPattern = pattern.replace(b'?P<id>', b'(?P=myFlag)', re.DOTALL)
print(replPattern)
contents = re.sub(pattern, replPattern, contents)
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)
{

}



More information about the Python-list mailing list