re.findall(r"(i).*\1", "i am says i am")
The only thing that's returned is one "i". I've searched for hours w/o finding a solution to this problem.
UPDATE: I was hoping to return "i am says i".
re.findall(r"(i).*\1", "i am says i am")
The only thing that's returned is one "i". I've searched for hours w/o finding a solution to this problem.
UPDATE: I was hoping to return "i am says i".
When using parenthesis (...)
in a regex
, only things inside them are captured, so in your case, only "i"s
are found.
That's the way findall
works, it will print capturing groups if they are present.
You can use re.search
:
>>> print re.search(r'(i).*\1', "i am says i am").group(0)
i am says i
Or use additional grouping in findall
as this:
>>> print re.findall(r'((i).*\2)', "i am says i am")[0][0]
i am says i