This may be an easy question, but I can't figure it out.
When I test the following string TEST_NO against
\b(?>TEST|TEST_NO)\b it doesn't match. When I test it against \b(?>TEST_NO|TEST)\b it works as expected. Can someone explain why?
Perhaps you intended to use a non-capturing group:
\b(?:TEST|TEST_NO)\b
You're using an atomic group. And atomic groups prevent backtracking.
An atomic group is a group that, when the regex engine exits from it, automatically throws away all backtracking positions remembered by any tokens inside the group.
This is how it goes:
The subpattern Text
\b(?>TEST|TEST_NO)\b TEST_NO
^^^^^^^^^ ^^^^
And then it tries to match:
The subpattern Text
\b(?>TEST|TEST_NO)\b TEST_NO
^^ ^
And it fails, so it tries to backtrack, but the atomic group prevents it.
You can also see a step-by-step debug in regex101.com (click on regex debugger).

This regex:
\b(?>TEST|TEST_NO)\b
will fail to match TEST_NO due to use of Atomic Group.
An atomic group automatically throws away all backtracking positions remembered by any tokens inside the group, when the regex engine exits from it.
Since this regex matches \bTEST first it will exit from the atomic group after this point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With