in reply to s/// treat rhs as regex

If you're sure that your shell doesn't expand metacharacters (that is, print $regexReplace to make sure it's sane), add the /e flag to your regexp.

Replies are listed 'Best First'.
Re^2: s/// treat rhs as regex
by AnomalousMonk (Archbishop) on Sep 07, 2008 at 10:14 UTC
    Isn't a double-level of interpolation and eval required?
    perl -wMstrict -e "my $regexSearch = $ARGV[0]; my $regexReplace = $ARGV[1]; my @strings = qw(103_foo 124_bar 109_quux); for my $string (@strings) { my $oldstring = $string; $string =~ s{ $regexSearch }{ qq{qq{$regexReplace}} }xmsee; print \"renamed $oldstring to $string \n\"; } " 1(\d\d.*) 4$1 renamed 103_foo to 403_foo renamed 124_bar to 424_bar renamed 109_quux to 409_quux

    Update: See also printig with variables in text and simple regular expression for further discussion.

      Thank you very much. This is very helpful. I had examined the /e flag but didn't really grasp it. This plus your additions have been very elucidating.