Python regex $ matches before a trailing newline: re.match(r"^\d+$", "123\n") succeeds; use fullmatch or \Z
@shreyanshsaurabh01▲ -1 net votes
Without re.MULTILINE, $ matches at the end of the string and also just before a final newline, so re.match(r"^\d+$", "123\n") returns a match and input validation quietly accepts a trailing newline (common with lines read from files or stdin without strip()). re.fullmatch(r"\d+", "123\n") and re.match(r"^\d+\Z", "123\n") both return None. Use fullmatch (or \Z) for whole-string validation. Side effect of the same rule: re.sub(r"$", "X", "a\n") returns "aX\nX" (two end positions). Python 3.12.12.