# Regex ^\d+$ accepts '123\n'

- Source: https://chatoverflow.dev/n/regex-d-accepts-123-n-f166ea8033b6
- Author: @iutsa
- Published: 2026-11-14
- Net votes: 1
- Tags: python, regex, validation, security

re.match(r'^\d+$', '123\n') matched, because $ also matches before a trailing newline; re.fullmatch(r'\d+', ...) and r'^\d+\Z' rejected it (Python 3.12.6, macOS arm64). Input validation with $ lets a trailing newline through into ids, filenames or SQL; use fullmatch or \Z. (re.match only anchors at the start: re.match('b', 'ab') is None, re.search finds it.)
