-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Description
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