[英]Python regex - Find and replace the second item of a pair
I've been trying to do the following : Given a char like "i", find and replace the second of every pair of "i" (without overlapping). 我一直在尝试执行以下操作:给定像“ i”这样的字符,找到并替换每对“ i”中的第二个(不重叠)。
"I am so irritated with regex. Seriously" -> "I am so rritated wth regex. Seriously".
I almost found a solution using positive lookbehind, but it's overlapping :( 我几乎找到了使用正向后看的解决方案,但是它是重叠的:(
Can anyone help me? 谁能帮我?
My best was this (I think) -> "(?<=i).*?(i)"
我最好的就是(我认为)-> "(?<=i).*?(i)"
EDIT : My description is wrong. 编辑:我的描述是错误的。 I am supposed to replace the SECOND item of a pair, so the result should've been: "I am so irrtated with regex. Serously" 我应该替换成对的SECOND项,因此结果应该是:“我对regex感到非常恼火。
Your regex matches overlapped substrings because of the lookbehind (?<=i)
. 由于后向(?<=i)
您的正则表达式匹配重叠的子字符串。 You need to use a consuming pattern for non-overlapping matches: 您需要对非重叠匹配使用消费模式:
i([^i]*i)
Replace with \\1
backreference to the text captured with ([^i]*i)
. 用\\1
向后引用替换([^i]*i)
捕获的文本。 See the regex demo . 参见regex演示 。
The pattern matches: 模式匹配:
i
- a literal i
, after matching it, the regex index advances to the right (the regex engine processes the string from left to right by default, in re
, there is no other option), 1 char i
一个文字i
,匹配它后,regex索引向右移 (默认情况下,regex引擎从左到右处理字符串, re
,没有其他选择),1个字符 ([^i]*i)
- this is Group 1 matching 0+ characters other than i
up to the first i
. ([^i]*i)
-这是第1组,匹配i
以外的0+个字符,直到第一个i
。 The whole captured value is inside .group(1)
. 整个捕获的值在.group(1)
内部。 After matching it, the regex index is after the second i
matched and consumed with the whole pattern. 匹配之后,正则表达式指数是第二后i
匹配,并与整个图案被消耗 。 Thus, no overlapping matches occur when the regex engine goes on to look for the remaining matches in the string. 因此,当正则表达式引擎继续查找字符串中的其余匹配项时,不会发生重叠的匹配项。 Python demo : Python演示 :
import re
pat = "i"
p = re.compile('{0}([^{0}]*{0})'.format(pat))
test_str = "I am so irritated with regex. Seriously"
result = re.sub(p, r"\1", test_str)
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.