Reputation: 29
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?
Upvotes: 2
Views: 1337
Reputation: 189387
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)
Upvotes: 4