in reply to Regex Help

Yes, some of this is shell dependant. I prefer using [x] over \x for escaping characters in regular expressions -- I just think it is easier to read (though it might make the regex slightly slower if the regex compiler doesn't optimize single-char classes -- I hadn't really worried about that before). It is unfortunate that the convoluted rules for detecting variable names inside regexes think [$] is trying to expand the variable $] so you can't use this trick to escape a dollar sign in a regexp.

In most Unix shells, you can switch between quoting styles to make your life easier:

perl -pi.orig -e 's#<% $flat->purge'"[(]'dynamix','outburst'[)] %>#"'$ +CONFIG{DYNAMIX_OUTBURST}#ge' www/index.html

Under Win32, you'd use:

perl -pi.orig -e "s#<% $flat->purge[(]'dynamix','outburst'[)] %>#$CONF +IG{DYNAMIX_OUTBURST}#ge" www/index.html

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
REx optimization of char class
by dchetlin (Friar) on Sep 23, 2000 at 05:25 UTC
    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.