Either it does not succeed (no matches at all), as it did before I had replaced the code blocks with the variables for them, or it matches more than it was expected to--depending on whether I have added the "eval" or not

This is one of the problems when you try to do everything in a single match. I mean, there are times when you do want a single match, but you need to consider what you want the regex engine to do when it encounters something that isn't matched. So, for instance, maybe you need some sort of catch-all that returns an "error token" before resuming trying to match the rest.

In many cases, when parsing things, I desire the regex to give me as many parse tokens as it was able to create and then tell me where the error is, so that I can report the error to the user with some context information. The easiest way to accomplish that is with the /gc regex flag and then the \G marker at the start of the regex to pick up where you left off. The location in the string is tracked per-string, and you can inspect it with the pos keyword.

my @tokens; while ($input =~ /\G( pattern... )/gcx) { push @tokens, $1; } if (pos $input < length $input) { say "Parse error at ...."; }
You can read all about it in perlre

In reply to Re: Repeated code blocks in long and hairy regex by NERDVANA
in thread Repeated code blocks in long and hairy regex by Polyglot

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.