A few comments. First {1} is useless as a literal char in a regex means {1} of. Next adding \s* at the end allows trailing whitepspace which is common. Next - it is better to specify what is legal than what is illegal as your regex will probably not catch all the nasty chars. Just allow through what you need by adding them to the $valid class. Using this method you know all the chars that are valid. If you drop the syntax check you can do the match/untaint in one line. Oh and you don't need to \ the ; as the 12 regex metachars are $^*()+{[|\.? Finally the /g does nothing as you are insisting on a match from the begining of the line to a newline (which in this case will be the end of the line - unless you have been messing with $/ :-)

my $valid = qr/[\w\s]/; while (<DATA>){ # grab: ween{(commands)} if (/^ween\s*\{\s*($valid+;)\s*\}\s*$/) { my $cmd = $1; print "Found $cmd\n"; } else { print "$0: Illegal chars or incorrect syntax!\n"; print "Line: $_\n"; } } __DATA__ ween { foo bar; } ween { del *.*; }

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: regex optimization by tachyon
in thread regex optimization by Anonymous Monk

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.