Click to See Complete Forum and Search --> : $1, $2, $3 does not get evaluated


ims.analyst
11-21-2005, 02:01 PM
Hi All,

I am a newbie at perl and I am facing a problem which I can not resolve. I have tried to find the answer, but have been unsuccessful in finding any answer anywhere.

Code Excerpt #1:
$line =~ s/(<field name=)"myfieldname"(.*?)(\/>)/$1 "newNameValue" $2 bLogged="true" $3/g;

Code Excerpt #2:
$findString = "(<field name=)\"myfieldname\"(\.\*\?)(\/>)";
$replaceString = "\$1 \"newNameValue\" \$2 bLogged=\"true\" \$3";
$line =~ s/$findString/$replaceString/g;

#1. Works beautifully and replaces the string the way I want.
However,
#2.does not evaluate $1, $2, $3 and replaces the string with these values, meaning: the string is replaced with:
$1 "newNameValue"$2 bLogged="true" $3

I have tried everything to evaluate these values, but nothing seems to work. Any help is really appreciated.

Thanks in advance,
Ims.

Jeff Mott
11-23-2005, 05:24 AM
Change the $replaceString to

$replaceString = q`$1 . '"newNameValue" ' . $2 . ' bLogged="true" ' . $3`;

and add the /e option to the regular expression. This will cause the replacement string to be evaluated as code.