Regular expression, greedy for |

for several weeks im working with regular expressions in php. Now my question is: is there any way to make RegEx greedy for |?

For example, the topic: 012345abcdefghijklm

pattern: /(abcde|abcdefghi)/will fetch abcdethough abcdefghi- this is a greedier match.

The only way I found is to sort RegEx by the longest possible matches

thank

+4
source share
3 answers

There is no other way than reordering items or adding an optional group without capturing.

. - , - Alternation (|), .

(/(abcdefghi|abcde)/) (/(abcde(?:fghi)?)/).

+2

:

abcde(?:fghi)?
0

Not a perfect solution. But it provides an alternative other than sorting:

(abcde(?!fghi)|abcdefghi)
0
source

All Articles