Alex Rivera | Logout

Capturing group with findall?

Asked 2011-05-16T13:43:40.940
108

How can I access captured groups if I do findall(r'regex(with)capturing.goes.here') ? I know I can do it through finditer, but I don't want to iterate.

Edit
Report

1 Answer

47

Use groups freely. The matches will be returned as a list of group-tuples:

>>> re.findall('(1(23))45', '12345')
[('123', '23')]

If you want the full match to be included, just enclose the entire regex in a group:

>>> re.findall('(1(23)45)', '12345')
[('12345', '23')]
answered 2011-05-16T13:51:21.867

Your Answer