Regex backreference findall not working

断了今生、忘了曾经 提交于 2019-12-17 20:39:01

问题


I have recently been using regexes in a program. In this program I used them to find words in a list of words that matched a certain RE. However, when i tried backreferencing with this program, I got an interesting result.

Here is the code:

import re
pattern = re.compile(r"[abcgr]([a-z])\1[ldc]")
string = "reel reed have that with this they"
print(re.findall(pattern, string))

What I expected was the result ["reel","reed"] (the regex matched these when I used it with Pythex)

However, when I ran the code using python (I use 3.5.1) I got the following result:

['e','e']

Please can someone with more experience with REs explain why I am getting this problem and what I can do to resolve it.

Thank you.


回答1:


The re.findall only returns captured values captured with capturing groups inside the regex pattern.

Use re.finditer that will keep the zeroth group (the whole match):

import re
p = re.compile(r'[abcgr]([a-z])\1[ldc]')
s = "reel reed have that with this they"
print([x.group(0) for x  in p.finditer(s)])

See the IDEONE demo



来源:https://stackoverflow.com/questions/37513320/regex-backreference-findall-not-working

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