Why does findall returns a blank string?

Viewed 350
Pattern = re.compile('.*', re.DOTALL)
Pattern.findall('Serve the public trust.\nProtect the innocent.\nUphold the law.')

It returns an extra blank string in the end.

Output = ['Serve the public trust.\nProtect the innocent.\nUphold the law.', '']

How can I prevent this?

1 Answers

This is a bug in some versions of Python. Technically, .* matches an empty string, too, so findall duly finds one after the other match.

The simple workaround is to use a regex which matches more than zero characters.

Pattern = re.compile('.+', re.DOTALL)

If you genuinely need to match empty strings, just not at the end of the string, you can add a(n ugly) lookahead assertion.

Pattern = re.compile('(?!$).*', re.DOTALL)
Related