I have a recent issue that required a regex with the or '|' operator and was confused on the output.

Here is some sample input:

this_line_'aaa|bbb|ccc|ddd|eee'_is_terminated this_line_'aaa|bbb|ccc|ddd|eee'_is_terminated this_line_'aaaaa|bbbbb|ccccc|ddddd|eeeee'_is_... this_line_'aaaaa|bbbbb|ccccc|ddddd|eeeee'_is_... this_line_'aaaaaa|bbbbbb|cccccc|dddddd|eeeeee... this_line_'aaaaaa|bbbbbb|cccccc|dddddd|eeeeee...

I am using the following code to find and extract part of the line:

while (<>) { $inline = $_; ($data,$trail) = $inline =~ /this_line_'(.*)('_|\.\.\.); print $data . "\n"; }

What I am getting is the following output:

aaa|bbb|ccc|ddd|eee aaa|bbb|ccc|ddd|eee aaaaa|bbbbb|ccccc|ddddd|eeeee'_is_ aaaaa|bbbbb|ccccc|ddddd|eeeee'_is_ aaaaaa|bbbbbb|cccccc|dddddd|eeeeee aaaaaa|bbbbbb|cccccc|dddddd|eeeeee

The first two lines I understand as they match the regex with only one of the second conditions. The last two lines I understand as well as they match the regex with only one of the second conditions.

However, the middle two lines I do not understand. The second regex condition with the '|' in it would seem to me to eval left to right. Rather, it seems to be being evaluated right to left. Maybe it is not really right to left but some other way.

Can anyone help me to understand the priority of the conditions in a regex where the or ('|') operator is concerned?


In reply to Priority of | regex operator by bfdi533

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.