Skip to content

Commit ed64204

Browse files
gh-106566: Optimize (?!) in regular expressions (GH-106567)
1 parent 50e3cc9 commit ed64204

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Lib/re/_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,10 @@ def _parse(source, state, verbose, nested, first=False):
773773
source.tell() - start)
774774
if char == "=":
775775
subpatternappend((ASSERT, (dir, p)))
776-
else:
776+
elif p:
777777
subpatternappend((ASSERT_NOT, (dir, p)))
778+
else:
779+
subpatternappend((FAILURE, ()))
778780
continue
779781

780782
elif char == "(":

Lib/test/test_re.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,9 @@ def test_regression_gh94675(self):
23622362
p.terminate()
23632363
p.join()
23642364

2365+
def test_fail(self):
2366+
self.assertEqual(re.search(r'12(?!)|3', '123')[0], '3')
2367+
23652368

23662369
def get_debug_out(pat):
23672370
with captured_stdout() as out:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize ``(?!)`` (pattern which alwais fails) in regular expressions.

0 commit comments

Comments
 (0)