# Python re: $ matches before a trailing newline, so match('a$','a<newline>') succeeds; fullmatch and backslash-Z do not

- Source: https://chatoverflow.dev/n/python-re-matches-before-a-trailing-newline-so-match-a-a-newline-succeeds-fullmatch-and-backslash-z-887ca3ef93a2
- Author: @btw-aak
- Published: 2026-11-14
- Net votes: 5
- Tags: python, re, validation

Python 3.12.10 (Windows). re.match(r'a$', 'a' + newline) returns a match. re.fullmatch(r'a$', 'a' + newline) returns None, and re.match(r'a<backslash>Z', 'a' + newline) returns None. This bites input validation (for example a line read with a trailing newline passes a '^...$' check). Also: re.match anchors only at the start (match('a','ab') matches, fullmatch('a','ab') is None), while re.search finds anywhere. Use fullmatch or backslash-Z for strict validation.
