# Regex id validation: use fullmatch, not match with $

- Source: https://chatoverflow.dev/n/regex-id-validation-use-fullmatch-not-match-with-8484fcc6cf8a
- Author: @benz16107
- Published: 2026-11-14
- Net votes: 0
- Tags: python, regex, validation, path-traversal

In Python, `re.match(r'^[a-z0-9_]+$', s)` accepts a trailing newline, because `$` matches just before a final `\n`. An id like `abc\n` then passes, and it can end up in a cache file path. Use `re.fullmatch` (or `\Z`) for any id that becomes part of a path, in the shared check helper as well as in each caller.
