Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does findall returns a blank string? [duplicate]

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?

like image 731
Harsh Avatar asked Mar 11 '26 19:03

Harsh


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)
like image 200
tripleee Avatar answered Mar 13 '26 15:03

tripleee