简体   繁体   English

不能在 re.sub() repr 表达式中的 function 调用中使用 '\1' 反向引用来捕获组

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

I have a string S = '02143' and a list A = ['a','b','c','d','e'] .我有一个字符串S = '02143'和一个列表A = ['a','b','c','d','e'] I want to replace all those digits in 'S' with their corresponding element in list A .我想用列表A中的相应元素替换 'S' 中的所有这些数字。

For example, replace 0 with A[0] , 2 with A[2] and so on.例如,将0替换为A[0] ,将2替换为A[2]等等。 Final output should be S = 'acbed' .最终 output 应该是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>' .但是,这会给出错误ValueError: invalid literal for int() with base 10: '\\g<1>' I guess it is considering backreference '\g<1>' as a string.我猜它正在考虑将反向引用'\g<1>'作为字符串。 How can I solve this especially using re.sub and capture-groups, else alternatively?我该如何解决这个问题,特别是使用re.sub和 capture-groups,或者?

The reason the re.sub(r'([0-9])',A[int(r'\\g<1>')],S) does not work is that \\g<1> (which is an unambiguous representation of the first backreference otherwise written as \\1 ) backreference only works when used in the string replacement pattern . re.sub(r'([0-9])',A[int(r'\\g<1>')],S)不起作用的原因是\\g<1> (这是一个明确的第一个反向引用的表示,否则写为\\1 )反向引用仅在字符串替换模式中使用时才有效。 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.如果你将它传递给另一个方法,它只会“看到” \\g<1>文字字符串,因为re模块当时没有任何机会评估它。 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. re引擎仅在匹配期间对其进行评估,但在re引擎尝试查找匹配之前评估A[int(r'\\g<1>')]部分。

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.这就是为什么可以在re.sub中使用回调方法作为替换参数的原因:您可以将匹配的组值传递给任何外部方法以进行高级操作。

See the re documentation :请参阅re文档

re.sub(pattern, repl, string, count=0, flags=0)

If repl is a function, it is called for every non-overlapping occurrence of pattern .如果repl是一个函数,则每次出现不重叠的pattern都会调用它。 The function takes a single match object argument, and returns the replacement string.该函数采用单个匹配对象参数,并返回替换字符串。

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查看Python 演示

Note you do not need to capture the whole pattern with parentheses, you can access the whole match with x.group() .请注意,您不需要使用括号捕获整个模式,您可以使用x.group()访问整个匹配项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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