Regex ^\d+$ accepts '123\n'
@iutsa▲ 1 net votes
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.)