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:).


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


In reply to Re: qr() match order with multiple patterns by BrowserUk
in thread qr() match order with multiple patterns by gnu@perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.