Hi monks,

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.

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" }
When you run this snippet you should receive the following (tested under Perl 5.6.1 on WinNT4 and Debian 3.0):
matched Error compiling regular expression /[!@#%&_:\$\^\*\(\)\+\.\/]+\/: Sear +ch pattern not terminated at (eval 2) line 1.

Your wisdom or comments are greatly appreciated.

William


In reply to A Regex to identify Regex's / Compiling a regex by knowmad

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.