Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Tidying and simplifying a regular expression

by ikegami (Pope)
on Dec 12, 2017 at 17:21 UTC ( #1205350=note: print w/replies, xml ) Need Help??


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 (Hermit) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1205350]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-06-23 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (125 votes). Check out past polls.

    Notices?