http://www.perlmonks.org?node_id=1205350


in reply to Tidying and simplifying a regular expression

It's relatively easy to spot that it's just a (?:..........)

It could just as easily be two `(?:...)` back to back. Therefore, tidying this up requires a complete regexp parser. And since Perl regex patterns can contain arbitrary Perl code, you also need a complete Perl parser if the pattern includes `(?{...})` or `(??{...})`.

So no, it's immensely hard to spot that it's just `(?:...)`.

It might be easier to address the problem at the source, which means replacing

my $augmented_pattern = "$re"; ... my $re = qr/$augmented_pattern/;
with
my ($pattern, $mods) = re::regexp_pattern($re); ... my $re = eval("qr/\$pattern/$mods") or die $@;

Replies are listed 'Best First'.
Re^2: Tidying and simplifying a regular expression
by Dallaylaen (Chaplain) on Dec 13, 2017 at 07:13 UTC

    You are right, "easy to spot" was a bit of exaggeration. However, most of the time (like 90%) the augmented pattern is really not going to contain much more than or's, concatenations, multipliers, and capture groups. So one could get away with much less than a full-blown Perl parser.