Can't use '\1' backreference to capture-group in a function call in re.sub() repr expression

巧了我就是萌 提交于 2019-12-11 06:00:41

问题


I have a string S = '02143' and a list A = ['a','b','c','d','e']. I want to replace all those digits in 'S' with their corresponding element in list A.

For example, replace 0 with A[0], 2 with A[2] and so on. Final output should be S = 'acbed'.

I tried:

S = re.sub(r'([0-9])', A[int(r'\g<1>')], S)

However this gives an error ValueError: invalid literal for int() with base 10: '\\g<1>'. I guess it is considering backreference '\g<1>' as a string. How can I solve this especially using re.sub and capture-groups, else alternatively?


回答1:


The reason the re.sub(r'([0-9])',A[int(r'\g<1>')],S) does not work is that \g<1> backreference only works when used in the string replacement pattern. If you pass it to another method, it will "see" just \g<1> literal string, since the re module won't have any chance of evaluating it at that time. re engine only evaluates it during a match, but the A[int(r'\g<1>')] part is evaluated before the re engine attempts to find a match.

That is why it is made possible to use callback methods inside re.sub as the replacement argument: you may pass the matched group values to any external methods for advanced manipulation.

Use

import re
S = '02143' 
A = ['a','b','c','d','e']
print(re.sub(r'[0-9]',lambda x: A[int(x.group())],S))

See the Python demo

Note you do not need to capture the whole pattern with parentheses, you can access the whole match with x.group().



来源:https://stackoverflow.com/questions/48697161/cant-use-1-backreference-to-capture-group-in-a-function-call-in-re-sub-rep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!