Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

python - Regex backreference findall not working

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...