4

for some weeks im working with regular expressions in php. Now my question: Is there any way, to make the RegEx greedy over | ?

for example subject: 012345abcdefghijklm

pattern: /(abcde|abcdefghi)/ will extract abcde, although abcdefghi is the greedier match.

The only way i found, is to sort the RegEx by the highest length of possibly matches

Thanks

Ravi Dhoriya ツ
  • 4,435
  • 8
  • 37
  • 48
guest1
  • 53
  • 4
  • did you want to capture `abcde` and `abcdefghi` into two separate groups? – Avinash Raj Aug 02 '14 at 15:07
  • The example from above is very simple. In fact its a bit harder. Its not like the second is exactly the same, like the first expression at the beginning. The first is a complete different expression, that accidentally matches the second too. – guest1 Aug 02 '14 at 15:07
  • What's your actual question is? – Avinash Raj Aug 02 '14 at 15:09
  • I just wanted to know, if there is something like a mode that captures the whole RegEx instead of finishing after the first match. (no, global is not what i meant) – guest1 Aug 02 '14 at 15:11
  • 1
    you could try this http://regex101.com/r/uI2oL4/1 to capture both strings in a seperate groups. – Avinash Raj Aug 02 '14 at 15:15
  • Define "greedy over |". For this specific case it's better to rearrange. If you meant skipping matches when one alternation is matched use atomic groups. – Unihedron Aug 02 '14 at 15:19

3 Answers3

2

There is no way other than reordering the elements or by adding an optional non-capture group.

Regular Expression engines are eager. Because of the way the Alternation meta-character works (|), the first possible match exits the alternation clause.

Either re-order the possible choices (/(abcdefghi|abcde)/) or use an optional non-capture group (/(abcde(?:fghi)?)/).

Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
0

Here you should use:

abcde(?:fghi)?
Andie2302
  • 4,825
  • 4
  • 24
  • 43
0

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

(abcde(?!fghi)|abcdefghi)
HamZa
  • 14,671
  • 11
  • 54
  • 75
HMK
  • 564
  • 6
  • 14