I'm trying to use some arithmetic over the matched patterns in perl
command line. I'm able to do it for one match but not for all.
str="a1b2c3"
perl -pe 's/\d+/$&+1/e' <<<"$str"
a2b2c3
I understand $&
refers to the first matched digit 1
here. What do I need to do to add 1
to all the digits? Is there a variable similar to $&
that represents all matched patterns? or the regex needs to be modified to match multiple digits.
For the given input, I'm expecting output something like
a2b3c4
Best Answer
The
g
flag to the substitution would make Perl apply the expression for each non-overlapping match on the input line.Nitpick: There are actually no capture groups involved here (the original question mentioned capture groups). The Perl variable
$&
is the "string matched by the last successful pattern match". This is different from e.g.$1
and$2
etc. that refer to the string matched by the corresponding capture group (parenthesised expression). There are no capture groups in\d+
, but you could have useds/(\d+)/$1+1/ge
instead, which does use a single capture group.There is no difference between
s/(\d+)/$1+1/ge
ands/\d+/$&+1/ge
in terms of outcome. In this short in-line Perl script, it makes no difference whether you choose to use one or the other, but generally you'd like to avoid using$&
in longer Perl programs that do many regular expression operations, at least if using an older Perl release.From
perldoc perlvar
(my emphasis):