python正则表达式:re.findall(r"(do|re|mi)+","mimi rere midore") [英] python regular expression: re.findall(r"(do|re|mi)+","mimi rere midore")

查看:44
本文介绍了python正则表达式:re.findall(r"(do|re|mi)+","mimi rere midore")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么这个正则表达式,

I couldn't understand why this regular expression,

re.findall(r"(do|re|mi)+","mimi rere midore"),

产生这个结果,

['mi', 're', 're'].

我的预期结果是 ['mimi', 'rere', 'midore']...

My expected result is ['mimi', 'rere', 'midore']...

然而,当我使用这个正则表达式时,

However, when I use this regular expression,

re.findall(r"(?:do|re|mi)+","mimi rere midore"),

它按预期生成结果.

你能告诉我两个正则表达式之间的区别吗?谢谢.

Can you tell me the different between two regular expressions? Thank you.

推荐答案

区别在于捕获组.对于捕获组,findall() 返回捕获的内容.如果没有捕获组,则返回整个匹配项.

The difference is in the capturing group. With a capturing froup, findall() returns only what was captured. Without a capturing group, the whole match is returned.

在您的第一个示例中,组 only 捕获两个字符,无论是否重复.在第二个例子中,整个匹配包括任何重复.

In your first example, the group only captures the two characters, repeated or not. In the second example, the whole match includes any repetitions.

re.findall() 文档 很清楚区别:

The re.findall() documentation is quite clear on the difference:

以字符串列表的形式返回字符串中模式的所有非重叠匹配项.[…] 如果模式中存在一个或多个组,则返回组列表;如果模式有多个组,这将是一个元组列表.

Return all non-overlapping matches of pattern in string, as a list of strings. […] If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

如果您的 (do|re|mi)+ 模式是更大模式的一部分,并且您希望 findall() only 返回完整的重复字符集,对两个字母的选项使用非捕获组,并在整个字符周围设置一个捕获组:

If your (do|re|mi)+ pattern is part of a larger pattern and you want findall() to only return the full repeated set of characters, use a non-capturing group for the two-letter options with a capturing group around the whole:

r'Some example text: ((?:do|re|me)+)'

这篇关于python正则表达式:re.findall(r"(do|re|mi)+","mimi rere midore")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆