in reply to qr() match order with multiple patterns
The problem is not that it is matching right to left. It is that the regex engine will find the earliest, ie. left-most match that it can.
Notionally, it starts at the first character of the string and tries each of the alternations in your regex, left to right. If none of them match at the first character, then it moves to the next character and tries each alternation again, left to right.
The problem with your regex is, that whilst the longer subexpressions won't match early in the strings you posted in your other post, the shorter ones will. So it finds the shorter match before the longer match.
You can prove that it isn't the ordering of the alternations by reversing their ordering. You will still find the leftmost possible match first.
It's not completely clear to me from the two examples you gave, exactly which bits you want to match in the second example, but I think the fix is to make sure that the bit you are trying to match is bounded by anchors of some sort, maybe a non-digit char or the start or end of string. Something like this.
my $Date = qr{(?:^|\D)($MMDDYYYY|$DDMMYYYY|$YYYYMMDD|$DDMMYY|$MMDD +YY|$DDMM|$MMDD)(?:\D|$)};
You might want to use different anchors, and you might need to apply them to the individual parts of the composite rather than the composite, but as I said, without a few more example of inputs and desired outputs your goal isn't exactly clear (to me:).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: qr() match order with multiple patterns
by gnu@perl (Pilgrim) on Jul 23, 2003 at 14:37 UTC |