本文介绍了正则表达式反向引用 findall 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在程序中使用正则表达式.在这个程序中,我使用它们在与某个 RE 匹配的单词列表中查找单词.然而,当我尝试用这个程序进行反向引用时,我得到了一个有趣的结果.

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.

代码如下:

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

我期望的是结果 ["reel","re​​ed"](当我将它与 Pythex)

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

但是,当我使用 python(我使用 3.5.1)运行代码时,我得到了以下结果:

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

['e','e']

请对 RE 有更多经验的人解释为什么我会遇到这个问题以及我可以做些什么来解决它.

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

谢谢.

推荐答案

re.findall 仅返回使用正则表达式模式内的捕获组捕获的捕获值.

使用 re.finditer 将保留第零组(整场比赛):

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)])

查看 IDEONE 演示

这篇关于正则表达式反向引用 findall 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:52