regexpcre2

Regexp find a match with priority order but same captured group


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?

https://regex101.com/r/bc8XQE/1


Solution

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

    .*?\K\bhello\b|.*?\K\bhey\b|.*?\K\bhi\b
    

    See a regex demo.

    If you really must have group 1, then you can use a branch reset group:

    (?|.*?\b(hello)\b|.*?\b(hey)\b|.*?\b(hi)\b)
    

    See another regex demo.