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.
| [reply] [d/l] [select] |
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
| [reply] |