问题:Python Regex立即替换组

有没有办法使用正则表达式语法直接替换所有组?

正常方式:

re.match(r"(?:aaa)(_bbb)", string1).group(1)

但我想实现以下目标:

re.match(r"(\d.*?)\s(\d.*?)", "(CALL_GROUP_1) (CALL_GROUP_2)")

我想从正则表达式刚刚捕获的组中立即构建新字符串。

Is there any way to directly replace all groups using regex syntax?

The normal way:

re.match(r"(?:aaa)(_bbb)", string1).group(1)

But I want to achieve something like this:

re.match(r"(\d.*?)\s(\d.*?)", "(CALL_GROUP_1) (CALL_GROUP_2)")

I want to build the new string instantaneously from the groups the Regex just captured.


回答 0

看一下re.sub

result = re.sub(r"(\d.*?)\s(\d.*?)", r"\1 \2", string1)

这是Python的正则表达式替换(替换)功能。替换字符串可以用所谓的反向引用(反斜杠,组号)填充,这些反向引用将被组匹配的内容替换。该组的计数与该group(...)函数的计数相同,即1,从,从左到右,通过打开括号开始。

Have a look at re.sub:

result = re.sub(r"(\d.*?)\s(\d.*?)", r"\1 \2", string1)

This is Python’s regex substitution (replace) function. The replacement string can be filled with so-called backreferences (backslash, group number) which are replaced with what was matched by the groups. Groups are counted the same as by the group(...) function, i.e. starting from 1, from left to right, by opening parentheses.


回答 1

公认的答案是完美的。我想补充一点,使用以下语法可能会更好地实现组引用:

r"\g<1> \g<2>"

用于替换字符串。这样,您就可以解决语法限制,在语法限制中,组后面可以跟数字。再说一次,这一切都存在于文档中,没有什么新鲜的,只是有时很难一眼看出来。

The accepted answer is perfect. I would add that group reference is probably better achieved by using this syntax:

r"\g<1> \g<2>"

for the replacement string. This way, you work around syntax limitations where a group may be followed by a digit. Again, this is all present in the doc, nothing new, just sometimes difficult to spot at first sight.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。