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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With