in reply to Re: Is RegEx in Perl not NFA anymore?
in thread Is RegEx in Perl not NFA anymore?

... the order of the alternatives separated by | likely doesn't matter.

Further to Laurent_R's ++post: The ordered alternation "first match wins" behavior of Perl 5 can be seen here:

c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'xxxABCDEyyy'; ;; print qq{captured '$1'} if $s =~ m{ (ABC|ABCD|ABCDE) }xms; " captured 'ABC'


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Is RegEx in Perl not NFA anymore?
by Laurent_R (Canon) on Oct 20, 2016 at 13:33 UTC
    Yes, AnomalousMonk, that's exactly it.

    Just for the sake of completeness, these are the two variants of alternations in Perl 6, adapting your example:

    > my $s = 'xxxABCDEyyy'; xxxABCDEyyy > say "captured $/" if $s ~~ / ABC | ABCD | ABCDE /; captured ABCDE > say "captured $/" if $s ~~ / ABC || ABCD || ABCDE /; captured ABC
    With a single pipe, the longest match wins (which means, BTW, that the engine must try all possibilities to figure out which is the best); with a double pipe, the first match wins.

    But that holds only if all possible matches start on the same atom:

    > say "captured $/" if $s ~~ / xABC | ABCD | ABCDE /; captured xABC
    Here, even though "ADCDE" is a longer match, "xABC" wins because it starts earlier in the string.
Re^3: Is RegEx in Perl not NFA anymore?
by perldigious (Priest) on Oct 19, 2016 at 21:32 UTC

    Indeed, thanks AnomalousMonk, that was poor wording on my part. I should have added an "in this case" or something similar at the end since I meant that statement to apply to the specific alternatives being checked for a match against the specific string given, not a general statement to say "the order of such alternatives never matters". I do go on to explain it checks the leftmost alternative first.

    For some reason I originally thought it would drag the first (leftmost) alternative through the entire string before trying the second alternative and so on. Probably a silly thing to have ever thought, especially when considering groups of alternatives contained within more complicated regular expressions.

    I love it when things get difficult; after all, difficult pays the mortgage. - Dr. Keith Whites
    I hate it when things get difficult, so I'll just sell my house and rent cheap instead. - perldigious