in reply to Improve processing time for string substitutions

Like I already told you in the CB:

my $re = join "|", map { quotemeta $_ } keys %entitylist; $re = qr/(?:$re)/; # ... $string =~ s/($re)/$entitylist{ $1 }/ge;

You might want to take a look at perlre for the /e switch.

Replies are listed 'Best First'.
Re^2: Improve processing time for string substitutions
by Anno (Deacon) on Apr 16, 2007 at 15:46 UTC
    Is there a reason for the non-capturing parens in "$re = qr/(?:$re)/;"?

    What good does the /e switch do in the regex "s/($re)/$entitylist{ $1 }/ge;"?

    Anno

      No (qr// already adds non-capturing parens) and none (interpolation is done after each match).

      The /e switch is an error by me - it is a leftover from when I thought about doing the hex-conversion manually with a sprintf call in the right hand side..

      I always build my regular expressions with noncapturing parentheses when I pal on latter assembling them - this prevents embarassing bug hunts later when I change how the target RE is built, possibly by repeating one ("atomic") building block - leaving out the parentheses causes hard-to-track misbehaviour with input on the seam of the two blocks.

      .
        I always build my regular expressions with noncapturing parentheses when I pal on latter assembling them - this ...

        Same here, but I've always thought of qr// as an alternative way to add those parens (which it does). You are, in effect, adding two pairs.

        Anno