Alexander Torstling Alexander Torstling - 1 month ago 5
Perl Question

Perl regex - match variable next to digit

I want to suffix a group match with a digit in a rexeg replace expression. How do I separate the match variable from the actal digit?

If I want

hello 55 friends -> hello 551 friend
i am 29 happy -> i am 291 happy


I tried with

s/([0-9]+)/$11/g;


But if I put a
1
directly after
$1
, perl interprets it as
$11
(rightfully so). In bash, I could have written
${1}1
, but this doesn't seem to work in perl.

Answer

As stated in the comments, the optional curly braces for backreferences (e.g. ${1} instead of $1) does work in perl.

Demo here