简体   繁体   English

为什么re.finditer似乎忽略了所提供的模式?

[英]Why does re.finditer seem to be ignoring the provided pattern?

Maybe I'm misunderstanding how re.finditer works, but it seems to be ignoring the pattern I give it and matching on all instances of the character. 也许我误解了re.finditer工作原理,但是它似乎忽略了我给它提供的模式,并在所有字符实例上都进行了匹配。 For example, this code: 例如,此代码:

word = "aaaa"
for match in re.finditer(r"(?<=a)a(?=a)", word):
    word = re.sub(match.group(), "b", word)

is giving me "bbbb" , when I would expect it to give "abba" , like this code does: 当我期望它给出"abba" ,给了我"bbbb" "abba" ,就像下面的代码一样:

word = "aaaa"
word = re.sub(r"(?<=a)a(?=a)", "b", word)

Any idea why matching through re.finditer seems to be working differently from doing it directly with re.sub ? 知道为什么通过re.finditer进行匹配似乎与直接使用re.sub进行工作有所不同吗?

EDIT: To clarify, what I'm actually trying to do is lookup what substitution to apply in a dictionary, but 编辑:为了澄清,我实际上想做的是查找要在字典中应用的替代方法,但是

word = re.sub(r"(?<=a)(a)(?=a)", d[r"\1"], word)

(where d is a dictionary) doesn't work because \\1 is not a key, so I was hoping to iterate through and apply substitutions as I find them. (其中d是字典)不起作用,因为\\ 1不是键,所以我希望在找到它们时进行迭代并应用替换。 I also can't just do a separate line for each substitution in the dictionary because I have a lot of changes that are circular, so a > b, b > c, and c > a, if that makes sense. 我也不能只为字典中的每个替换单独写一行,因为我有很多圆形的更改,因此a> b,b> c和c> a(如果有的话)。 In other words, if I try to apply the changes linearly, I'm going to end up back where I started. 换句话说,如果我尝试线性地应用更改,那么我将回到起点。 Maybe I'm approaching this the wrong way? 也许我走错路了?

In the first case, the value of match.group() is "a" at both iterations. 在第一种情况下,两次迭代中match.group()的值均为“ a”。 word = re.sub(match.group(), "b", word) becomes word = re.sub("a", "b", word) , which naturally replaces each "a" with a "b". word = re.sub(match.group(), "b", word)变为word = re.sub("a", "b", word) ,自然会将每个“ a”替换为“ b”。

In the second example, only the middle "a"'s are replaced. 在第二个示例中,仅替换中间的“ a”。

I found the answer I was looking for here: Dictionary lookup with key as a matched group in python re.sub module . 我在这里找到了想要的答案: 在python re.sub模块中将关键字作为匹配组进行字典查找

I didn't realize you can pass in a function as the second parameter to sub , so essentially, all you have to do is something like this to incorporate dictionary lookup into the substitution: 我没有意识到您可以将函数作为第二个参数传递给sub ,因此本质上,您要做的就是将字典查询合并到替换中:

word = re.sub(r"(?<=a).(?=a)", lambda x: d[x.group()], word)

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

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