sethmlarson.dev

Archives
Log in
January 27, 2026

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:

Add a comment:

You're not signed in. Posting this comment will subscribe you to this newsletter with the email address you enter below.
Share this email:
Share on Hacker News Share on Reddit Share via email Share on Mastodon Share on Bluesky
sethmlarson.dev
bsky.app
mastodon.social
Powered by Buttondown, the easiest way to start and grow your newsletter.