Regex character “$” doesn’t mean “end-of-string”

The author discusses surprising behavior in Python’s regex module while developing SBOM tooling for CPython. They explain how the $ character can match the end of a string or a trailing newline depending on the platform, which can lead to unexpected results when trying to match strings without newlines. They also introduce alternatives like z and Z for end-of-string matching. The comparison table shows how different platforms handle this behavior. The key takeaway is that matching trailing newlines is consistent across platforms, but to avoid them, different approaches are needed. This article offers valuable insights into regular expressions and encourages further exploration.

https://sethmlarson.dev/regex-$-matches-end-of-string-or-newline

To top