Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex expression doesn't work in ascending order.

Tags:

regex

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?

like image 776
Shak Ham Avatar asked Dec 21 '25 13:12

Shak Ham


2 Answers

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).

regex101 debug

like image 63
Mariano Avatar answered Dec 24 '25 02:12

Mariano


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.

like image 24
anubhava Avatar answered Dec 24 '25 02:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!