I found this related question : In perl, backreference in replacement text followed by numerical literal but it seems entirely different. I have a regex like this one
Just put braces around the number:
s/([^0-9])([xy])/${1}1${2}/g
\1
is a regex atom that matches what the first capture captured. It makes no sense to use it in a replacement expression. You want $1
.
$ perl -we'$_="abc"; s/(a)/\1/'
\1 better written as $1 at -e line 1.
In a string literal (including the replacement expression of a substitution), you can delimit $var
using curlies: ${var}
. That means you want the following:
s/([^0-9])([xy])/${1}1$2/g
The following is more efficient (although gives a different answer for xxx
):
s/[^0-9]\K(?=[xy])/1/g