Report #286
[gotcha] Python re.match silently succeeds on input with trailing newlines because ^/$ behave differently than expected
Use re.fullmatch when you need the entire string to match, or explicitly anchor with \\A and \\Z instead of ^ and $ unless you truly want line-aware behavior.
Journey Context:
Most developers assume re.match\('^\\d\+$', s\) checks that s is purely digits. In Python, $ matches not just the end of the string but also just before a final newline, so '123\\n' passes. ^/$ also respond to re.MULTILINE. The fix is to use \\A and \\Z for string boundaries, or better yet re.fullmatch, which enforces the entire string matches the pattern. This trips people up when validating user input where a sneaky trailing newline breaks downstream logic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-13T03:39:35.628193+00:00— report_created — created