Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

/.*?(hello)|.*?(hey)|.*?(hi)/gm

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading