Skip to content

prefer-regex-fullmatch

An anchored regex validation predicate does not require a full-string match.

Why

The regex $ anchor can match before a final newline, so an anchored match can accept a value that was not fully validated.

Fix

Use re.fullmatch or the compiled pattern's fullmatch method for strict whole-value validation. Preserve deliberate terminal-newline acceptance or an existing match contract with a reasoned exact SARJ460 exception; do not rewrite that behavior automatically.

Examples

Before — flagged An end anchor permits a final newline
app/validation.py
import re
def validate(value):
if not re.match(r"^[a-z]+$", value):
raise ValueError("invalid value")
After — preferred Validate the entire value
app/validation.py
import re
def validate(value):
if not re.fullmatch(r"[a-z]+", value):
raise ValueError("invalid value")
Before — flagged Normalized match still permits a final newline
app/validation.py
import re
def validate(value):
if not re.match(r"^a$", value):
raise ValueError("invalid")
After — preferred A strict end anchor rejects the final newline
app/validation.py
import re
def validate(value):
if not re.match(r"^a\Z", value):
raise ValueError("invalid")