knowmad has asked for the wisdom of the Perl Monks concerning the following question:
I am working on some modifications to the Data::FormValidator module and have a couple of questions about regex's. The first is regarding a gnarly regex which I am trying to understand and the second is about compiling a regex into an anonymous subroutine.
I've put the regex which matches regex's into extended format and started adding some comments to figure out what this beast is doing. Any additional info would be helpful.
$constraint =~ m@ ^\s* # skip all whitespace at beginning ( /.+/ # capture the custom regex |m(.).+\2 # does this capture the ops? What does the |m do? + why is the (.) and \2 in there? ) [cgimosx]* # ?? don't understand what this is for. what are we +trying to match? I thought the ops were matched above \s*$ # skip all whitespace at end @x
The Data::FormValidator allows users to submit custom regex's for checking input. It does this by compiling the regex into an anonymous subroutine (see code below). I have discovered a curious anomaly in the evaluation phase of the subroutine when dealing with a regex that contains a \\ (an escaped backslash). Basically, one regex ($constraint_good) will compile and run while the second will not compile. The only difference between the two is the placement of the \\. The working regex puts this at the beginning; the bad one has this at the end (don't even ask how I figured this out; one of those flukes). See the code below for details.
When you run this snippet you should receive the following (tested under Perl 5.6.1 on WinNT4 and Debian 3.0):my $input = 'http://www.knowmad.com/'; my $constraint_good = '/[\\!@#%&_:\$\^\*\(\)\+\.\/]+/'; my $constraint_bad = '/[!@#%&_:\$\^\*\(\)\+\.\/]+\\/'; if ( $constraint_good =~ m@^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$@ ) { my $sub = eval 'sub { $_[0] =~ '. $constraint_good . '}'; die "Error compiling regular expression $constraint_good: $@" +if $@; print "matched\n"; } else { print "no good.\n" } if ( $constraint_bad =~ m@^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$@ ) { my $sub = eval 'sub { $_[0] =~ '. $constraint_bad . '}'; die "Error compiling regular expression $constraint_bad: $@" if $@; print "matched\n"; } else { print "no good.\n" }
matched Error compiling regular expression /[!@#%&_:\$\^\*\(\)\+\.\/]+\/: Sear +ch pattern not terminated at (eval 2) line 1.
Your wisdom or comments are greatly appreciated.
William
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A Regex to identify Regex's / Compiling a regex
by BrowserUk (Patriarch) on Sep 19, 2002 at 21:30 UTC | |
by knowmad (Monk) on Sep 19, 2002 at 22:01 UTC | |
by bart (Canon) on Sep 19, 2002 at 22:41 UTC | |
by BrowserUk (Patriarch) on Sep 19, 2002 at 22:47 UTC | |
Re: A Regex to identify Regex's / Compiling a regex
by zigdon (Deacon) on Sep 19, 2002 at 21:41 UTC | |
Re: A Regex to identify Regex's / Compiling a regex
by sauoq (Abbot) on Sep 19, 2002 at 22:30 UTC | |
Re: A Regex to identify Regex's / Compiling a regex
by mirod (Canon) on Sep 20, 2002 at 06:03 UTC |