re.findall not returning full match?

The problem you have is that if the regex that re.findall tries to match captures groups (i.e. the portions of the regex that are enclosed in parentheses), then it is the groups that are returned, rather than the matched string.

One way to solve this issue is to use non-capturing groups (prefixed with ?:).

>>> import re
>>> s="size=50;size=51;"
>>> re.findall('size=(?:50|51);', s)
['size=50;', 'size=51;']

If the regex that re.findall tries to match does not capture anything, it returns the whole of the matched string.

Although using character classes might be the simplest option in this particular case, non-capturing groups provide a more general solution.

Leave a Comment