$s =~ s/(id=")(".*)/$1123$2/;
Although you expected it to replace $1 , perl sees it as a $1123 variable. Perl doesn't know what you meant $1 . Therefore, you need to limit the variability to $1 by specifying it as ${1} :
$s =~ s/(id=")(".*)/${1}123$2/;
It is always a good idea to include the following at the top of your scripts. They will save you a lot of time and effort.
use strict; use warnings;
For example, running a script with the above modules results in an error message:
Use of uninitialized value $1123 in concatenation (.) or string at /tmp/test.pl line 7.
(ignore the reported script names and line numbers.) It clearly states what perl expected.
Another approach using look-behind and look-ahead statements:
$s =~ s/(?<=id=")(?=")/123/;
source share