Search code examples
pythonregexfindall

Regex missing matches?


I'm doing a matching problem with regexs in python and am using code similar to the following:

match = re.findall(r'a(.*)', 'xayaz')

I would have expected an output match = [ 'yaz', 'z'] but instead the actual output is match = ['yaz'].

Similarly I would expect re.findall(r'.*a(.*), 'xayaz') to return the same 2 matches but instead we get ['z'].

So my question is why is each of these regex's missing one match that I would expect it to hit? Or am I misunderstanding the findall method?

(first question, apologies for any mistakes, and thanks for any help!)


Solution

  • Use a lookahead to not "consume" any of the match so that you can get overlapping matches

    re.findall(r'(?=a(.*))', 'xayaz')