Here's the one that inspired me to ask:

$sql = " create table #foo (variable1 char(20), variable2 char(30) null) /* comments make it slow */ "; my $comment = qr/(?:\/\*.*?\*\/)/; # Simple +match of C-style comment my $sp = qr/(?:\s*$comment?\s*)*/; # This ju +st looks for /* comments */, with whitespace on either side. # Sinc +e comments can be anywhere, use this anywhere whitespace is allowed. # The parens don't capture +, so it won't affect your $1, $2 counts. my $header = qr/create${sp}table${sp}\#?\w+${sp}\(${sp}/i; # "create + table foo (", where table name may have a # mark my $name = qr/[\#\w]+${sp}/; # Variabl +e name, can contain # mark my $type = qr/\w+${sp}/; # Variabl +e type (e.g. 'char', 'number') my $op_char = qr/(?:\([\d\s,]+\)${sp})?/; # Bounds +for array types, can be multidimensional (e.g. '(10, 20)', '(30)') my $words = qr/(?:[\w]+(?:\([^\)]*\))?[${sp})]*)*/; # Text af +ter variable type -- may be 'null', 'not null', a 'check' block, etc. my $subst = qr/${sp}\{[^\}]+\}${sp}/; # Substit +ution placeholder -- some text in {curlies} my $decl = qr/($name $type $op_char $words)/x; # A compl +ete variable declaration my $comma = qr/,${sp}/; # Literal + comma, with possible trailing space my $rest = qr/\)${sp}/; # Closing + the block with a close-paren if ($sql =~ / ($header) ((?: $decl $comma | $subst $comma | $subst)* (?: $decl | $subst)) ($rest) /x) { print "Match!\n"; print "1: >>>$1<<<\n"; print "2: >>>$2<<<\n"; }
It takes about 45 seconds on my box. The longer the comment, the longer the runtime.

---
A fair fight is a sign of poor planning.


In reply to Re^2: Analyzing regular expression performance by Sprad
in thread Analyzing regular expression performance by Sprad

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.