Python regular expression findall *

两盒软妹~` 提交于 2019-12-02 13:24:07

The answer is simplified in the Regex Howto

As you can read here, group returns the string matched by the Regular Expression.

group() returns the substring that was matched by the RE.

But the action of findall is justified in the documentation

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

So you are getting the matched part of the capture group.

Some experiments include :

>>> r = re.compile(r'(b)(e)*')
>>> r.findall(text)
[('b', 'e')]

Here the regex has two capturing groups, so the returned values are a list of matched groups (in tuples)

When a pattern contains a capture group, findall returns only the content of the capture group and no more the whole match.

If this behaviour looks strange, it can be very useful to extract easily parts of a string in a particular context (substring before or after), especially since python re module doesn't support variable length lookbehinds.

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