Question

>>> match = re.findall('a.*?a', 'a 1 a 2 a 3 a 4 a')
>>> match
['a 1 a', 'a 3 a']

How do I get it to print

['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

Thank you!

Was it helpful?

Solution

I think using a positive lookahead assertion should do the trick:

>>> re.findall('(?=(a.*?a))', 'a 1 a 2 a 3 a 4 a')
['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

re.findall returns all the groups in the regex - including those in look-aheads. This works because the look-ahead assertion doesn't consume any of the string.

OTHER TIPS

r = re.compile('a.*?a') # as we use it multiple times
matches = [r.match(s[i:]) for i in range(len(s))] # all matches, if found or not
matches = [m.group(0) for m in matches if m] # matching string if match is not None
print matches

gives

['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']

I don't know if it is the best solution, but here I test every substring reaching to the end of the string for matching with the given pattern.

You may use alternative regex module which allows overlapping matches:

>>> regex.findall('a.*?a', 'a 1 a 2 a 3 a 4 a', overlapped = True)
['a 1 a', 'a 2 a', 'a 3 a', 'a 4 a']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top