in reply to Re^3: out of two strings which occurs first in a text
in thread out of two strings which occurs first in a text

That behaviour surprised me so I checked it out. You're right, but I would have expected the greediness of perls's regexp matching to favour the longer string. Interesting.
  • Comment on Re^4: out of two strings which occurs first in a text

Replies are listed 'Best First'.
Re^5: out of two strings which occurs first in a text
by ikegami (Patriarch) on Sep 24, 2008 at 20:28 UTC
    It's not a question of greediness. "pat" matched, so the alternation was successful. "pat" can't possible match any more than it has, so that's the end of the alternation. Elements of alternations are tried in turn. Greediness would factor in if you had /pat(?:tern)?/.

    If you wanted to get the longest first, sort the elements of the alternation.

    my ($re) = map qr/$_/, join '|', map quotemeta, sort { length($b) <=> length($a) } @words;

    Of course, that doesn't work for patterns that can match strings of various lengths.