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.
You can read all about it in perlremy @tokens; while ($input =~ /\G( pattern... )/gcx) { push @tokens, $1; } if (pos $input < length $input) { say "Parse error at ...."; }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |