简体   繁体   中英

Group in Python regex substitution

Why does this code return 'static PyObject*\\npy_\\x00(void)\\n{' ? I thought it should look for the first group and replace it. Also, I don't see where the \\x00 comes from.

re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',r'static PyObject*\npy_\0(void)\n{','def myfunc():')

Looks like the example copied from the docs for re , except you changed one piece. You have:

r'static PyObject*\npy_\0(void)\n{'

It should be (use \\1 for the first group):

r'static PyObject*\npy_\1(void)\n{'

When you use \\0 , that is interpreted as the escape for null \\x00 . If you really want group 0 (the entire substring matched by the re), you need to use \\g<0> .

\\0 does not reference the matched pattern. It should be \\g<0>

r'static PyObject*\npy_\g<0>(void)\n{'

This results in

static PyObject*\npy_def myfunc():(void)\n{

If you want to replace the first captured group, you could use \\g<1> , but \\1 will also work.

I thought it should look for the first group and replace it.

It doesn't. Python's re.sub doesn't look for the first group and replace it, it replaces the entire matched pattern. It's up to you to group and re-insert the parts you want to keep.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM