Unfortunately, Perl doesn't optimize [x]:

[~] $ perl -Mre=debug -wle'"x" =~ /x/' Freeing REx: `,' Compiling REx `x' size 3 first at 1 rarest char x at 0 1: EXACT <x>(3) 3: END(0) anchored `x' at 0 (checking anchored isall) minlen 1 Omitting $` $& $' support. EXECUTING... Guessing start of match, REx `x' against `x'... Found anchored substr `x' at offset 0... Guessed: match at offset 0 Freeing REx: `x' [~] $ perl -Mre=debug -wle'"x" =~ /[x]/' Freeing REx: `,' Compiling REx `[x]' size 10 first at 1 1: ANYOF[x](10) 10: END(0) stclass `ANYOF[x]' minlen 1 Omitting $` $& $' support. EXECUTING... Matching REx `[x]' against `x' Setting an EVAL scope, savestack=3 0 <> <x> | 1: ANYOF[x] 1 <x> <> | 10: END Match successful! Freeing REx: `[x]'

And in case that doesn't convince you:

[~] $ perl -MBenchmark -we'timethese(-5,{x=>q{"x"=~/x/},"[x]"=>q{"x"=~ +/[x]/}})' Benchmark: running [x], x, each for at least 5 CPU seconds... [x]: 4 wallclock secs ( 5.23 usr + 0.00 sys = 5.23 CPU) @ 85 +2787.57/s (n=4460079) x: 6 wallclock secs ( 5.25 usr + 0.00 sys = 5.25 CPU) @ 14 +99188.19/s (n=7870738)

P.S. I think you missed a backwhack in your RExen above; Perl is going to try to interpolate $flat.


In reply to REx optimization of char class by dchetlin
in thread Regex Help by meonkeys

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.