0

I'm trying to figure out the exact behavior of the conditional logic in Regex with Python. For this, I have made up a test that works as I would have expected in Regex101 using the Python flavor, but not in Python. The pattern is:

(hello)?(?(1) world| bye)

And test cases:

hello world
hello bye
something bye

In Regex101 this matches hello world in full and the bye in something bye.

In Python, this only matches hello world - I would have expected it to match the bye in something bye too?

Would be amazing if someone could help me understand where I'm misunderstanding this implementation? In Python, my code is:

tests = ['hello world', 'something bye', 'something world']

for t in tests:
    print(re.match("(hello)?(?(1) world| bye)", t))
James Briggs
  • 854
  • 6
  • 13

0 Answers0