I have the same problem as the one in the link but in PCRE2 (regex101.com):
Regexp find a match with priority order?
I have:
/.*?(hello)|.*?(hey)|.*?(hi)/ms
hi hey hello
But the problem is that when it finds hello
, it is stored in group 1, when it finds hey
in group 2, and when it finds hi
in group 3, I want only group 1 to be used instead.
How do I get this?
Using PCRE, instead of 3 different groups, you can use your pattern with the 3 alternatives and then make use of
\K
to forget what is matched so far.The word boundary
\b
prevents a partial word match.See a regex demo.
If you really must have group 1, then you can use a branch reset group:
See another regex demo.