Skip to content

re patterns with improperly positioned Line Anchors ($,^) don't error during compilation, yield false positives when matched against the empty string (e.g: .match("")) #127644

@anabelle2001

Description

@anabelle2001

Bug report

Bug description:

Summary

The re library in Python does not raise an error when attempting to parse an unmatchable that incorrectly uses line anchors ($ and ^).

re.compile("$$")  # Expected: Compilation Error
re.compile("^^") Expected: Compilation Error 

Furthermore, any regex composed entirely of line anchors will always match the empty string:

print(bool(re.compile("$$").match("")))   # Expected: Compilation Error, Got: True
print(bool(re.compile("$$$$$$").match(""))) # Expected: Compilation Error, Got: True

Expected Behavior

Compilation of invalid patterns using $ and ^ (e.g., "$^", "$$", "$$$$$$", etc.) should raise an error during re.compile().
Patterns containing only $ and ^ should not match an empty string unless explicitly designed to do so (e.g., via .* or equivalent).

Actual Behavior

Patterns with improper usage of line anchors ($ and ^) compile without error.
All patterns consisting only of $ and ^ unexpectedly match an empty string, when the only patterns in that group that should match are /$/, /^/, and /^$/

Suggested Fix

Update the re library to strictly validate anchor usage during re.compile() and raise errors for invalid patterns.
Ensure that patterns like "$^", "$$", and "$$$$$$" behave consistently and intuitively, aligning with common regex behavior.

More Test Cases

print(bool(re.compile("$$").match("")))   # Expected: Compilation Error, Got: True
print(bool(re.compile("$$$$$$").match(""))) # Expected: Compilation Error, Got: True
print(bool(re.compile("$$$$$$.*").match(""))) # Expected: Compilation Error, Got: True
print(bool(re.compile("$$$$$$.*").match("a"))) # Expected: False, Got: False
print(bool(re.compile("$$$$^^$$").match("")))  # Expected: Compilation Error, Got: True

here's an example of how other invalid regexes are handled:

print(bool(re.compile("[[").match(""))) # Expected: Compilation Error, Got: FutureWarning and Exception

CPython versions tested on:

3.10

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions