I’m expecting a string NOT to match a regular expression, but it is!
>>> re.compile('^P|([LA]E?)$').match('PE').group()
'P'
This seems like a bug, because I see no way for the $ to match. On the other hand, it seems unlikely that Python’s re lib would not be able to handle this simple case. Am I missing something here?
btw, Python prints this out when I start it:
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
Two other points worth mentioning:
^is redundant when you usere.match(), and if you want to match the end of the string, use r"\Z", not"$".Why $ is evil: Suppose we want to check that the whole of some string
smatches some patternfoo. We don’t want to call it a match if there is anything after the pattern. Let’s see what happens if there is a newline after the pattern.Here are excerpts from the docs:
Docs for
$: Matches the end of the string or just before the newline at the end of the string … this is using the default modes, and is nothing to do with MULTILINE mode.Docs for
\Z: Matches only at the end of the string. Nothing to do with modes, and not spooked by newlines.