Use “\A...\z”, not “^...$” with Python regular expressions
Two years ago I discovered a potential foot-gun with the Python standard library “re” module. I blogged about this behavior, and turns out that I wasn't only one who didn't know this: The article was #1 on HackerNews and the most-read article on my blog in 2024. In short the unexpected behavior is that the pattern “^Hello$” matches both “Hello” and “Hello\n”, and sometimes you don't intend to match a trailing newline.
Now in Python 3.14, we’re one step closer to a universal standard.
Read more: https://sethmlarson.dev/use-backslash-A-and-z-not-%5E-and-%24-with-python-regular-expressions
Don't miss what's next. Subscribe to sethmlarson.dev:
Share this email:
Add a comment: