Rondevous added the comment:
I was frustrated for hours when I couldn't figure out why this won't match:
>>> re.findall(r'(foo)?bar|cool', 'cool')
Now I know, I have to make this change: (?:foo)
But this isn't obvious.
Should it be mentioned in the docs of re.findall() to use (?:...) for
赵豪杰 <1292756...@qq.com> added the comment:
AhAh, got it, I misunderstood the usage, the findall returns tuple of groups
the expression set. Thanks @serhiy.storchaka @rhettinger
--
resolution: -> not a bug
stage: -> resolved
status: open -> closed
Raymond Hettinger added the comment:
When groups are present in the regex, findall() returns the subgroups rather
than the entire match:
>>> mo = re.search('(12)+', '121212 and 121212')
>>> mo[0] # Entire match
'121212'
>>> mo[1] # Group
Serhiy Storchaka added the comment:
It looks correct to me. Of course, the result is different, because they are
different functions. re.match() and re.search() return a match object (or
None), and re.findall returns a list. What result did you expect?
--
nosy: +serhiy.storchaka
New submission from 赵豪杰 <1292756...@qq.com>:
```
>>> import re
>>> text = '121212 and 121212'
>>> pattern = '(12)+'
>>> print(re.findall(pattern, text))
['12', '12']
>>>
>>>
>>> print(re.search(pattern, text))
>>>
>>>
>>> print(re.sub(pattern, '', text))
and
# The re.findall have