It's necessary to leave off the first .+ entirely and sort all the captured alternations by length decreasing (or some similar approach). Just making the first .+ non-greedy is not sufficient to find the longest match anywhere in the string:
>perl -wMstrict -lE "say q{'}, /^.+?(s m e f|s f pl|s f).+$/, q{' at offset }, $-[1] for '-s f s f pl s m e f-'; " 's f' at offset 1
In reply to Re^3: match longest sequence in an "or" RE
by AnomalousMonk
in thread match longest sequence in an "or" RE
by silentius
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |