in reply to Regexes that use a string as basis for matching

you mean like
$s = "match"; $re = qr/$s/; print "yup" if $something =~ $re;
or even simpler (but slower)
$s = "match"; print "yup" if $something =~ /$s/;


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: Regexes that use a string as basis for matching
by ikegami (Patriarch) on Jun 15, 2008 at 05:35 UTC
    The second snippet shouldn't be any slower. In both snippets, the regexp is compiled once.

    However,

    my @regexps = ( 'pattern1', 'pattern2', ); for my $s (@strings) { for $re (@regexps) { something($s =~ /$re/); } }

    is slower than

    my @regexps = ( qr/pattern1/, qr/pattern2/, ); for my $s (@strings) { for $re (@regexps) { something($s =~ /$re/); } }

    since the former compiles each regexp for each string, while the latter compiles each regexp once.

Re^2: Regexes that use a string as basis for matching
by Your Mother (Archbishop) on Jun 15, 2008 at 04:39 UTC
    or even simpler (but slower)

    Why is it slower?

Re^2: Regexes that use a string as basis for matching
by gatito (Novice) on Jun 14, 2008 at 22:05 UTC
    oooh thanks, that worked perfectly