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

Add another possible bug.   If, for example,  $first contains  'pat' and  $second contains  'pattern' then  $first will always match first.

Replies are listed 'Best First'.
Re^4: out of two strings which occurs first in a text
by wol (Hermit) on Sep 24, 2008 at 13:35 UTC
    True, but this is one of those cases where the bug is in the requirements as much as the implementation - what is the desired result in this case?

    --
    .sig : File not found.

Re^4: out of two strings which occurs first in a text
by dec (Beadle) on Sep 24, 2008 at 13:39 UTC
    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.
      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.