Part of your problem is you are using double-quotes when you should be using single-quotes so that, for instance, the $1 isn't interpolated at the time you initialise the $replace variable. Secondly, you need to use the e flag rather than the g flag because you need to execute a bit of code in your replacement part. In fact you need to use a double ee so that execution is done twice, first to interpolate the $replace variable into the "replace" term and second to interpolate again to get what was captured in $1. Thirdly, you have the terms of the substitution round the wrong way; "search" first, "replace" second.
$ perl -le ' > $search = q{(.*)\d{6}\.txt$}; > $replace = q{ qq{XXXX_$1} }; > $to_modify = q{AAAA200912.txt}; > print $to_modify; > $to_modify =~ s/$search/$replace/ee; > print $to_modify;' AAAA200912.txt XXXX_AAAA
I hope this is helpful.
Cheers,
JohnGG
In reply to Re: Passing $1 as part of a variable value to a regex search and replace
by johngg
in thread Passing $1 as part of a variable value to a regex search and replace
by zow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |