Why Python findall () and finditer () return empty matches in unanchored. * Search?

Python findall() for findall() and finditer() indicate that:

Empty matches are included in the result if they do not relate to the start of another match.

This can be demonstrated as follows:

 In [20]: [m.span() for m in re.finditer('.*', 'test')] Out[20]: [(0, 4), (4, 4)] 

Can someone tell me why this pattern returns an empty match in the first place? Shouldn't .* Consume the entire string and return a single match? And further, why at the end there is no empty match if I bind the template to the beginning of the line? eg.

 In [22]: [m.span() for m in re.finditer('^.*', 'test')] Out[22]: [(0, 4)] 
+5
source share
1 answer
  • .* is zero or more, so when four characters are consumed, an empty string with a zero length at the end (which does not apply to the start of any match) still remains; and
  • An empty line at the end does not match the pattern - it does not start at the beginning of the line.
+8
source

Source: https://habr.com/ru/post/1201749/


All Articles