diff --git a/Lib/re/_parser.py b/Lib/re/_parser.py index 22d10ab6e31..d00b7e67d55 100644 --- a/Lib/re/_parser.py +++ b/Lib/re/_parser.py @@ -773,8 +773,10 @@ def _parse(source, state, verbose, nested, first=False): source.tell() - start) if char == "=": subpatternappend((ASSERT, (dir, p))) - else: + elif p: subpatternappend((ASSERT_NOT, (dir, p))) + else: + subpatternappend((FAILURE, ())) continue elif char == "(": diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index a6f5af17d7d..a565cbe1a8d 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -2362,6 +2362,9 @@ class ReTests(unittest.TestCase): p.terminate() p.join() + def test_fail(self): + self.assertEqual(re.search(r'12(?!)|3', '123')[0], '3') + def get_debug_out(pat): with captured_stdout() as out: diff --git a/Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst b/Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst new file mode 100644 index 00000000000..3b88dc79183 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-09-13-10-54.gh-issue-106566.NN35-U.rst @@ -0,0 +1 @@ +Optimize ``(?!)`` (pattern which alwais fails) in regular expressions.