Reputation: 21
I want to increment the occurrence of all the digits in a string perl eg. if I have string as $str = "go to page number 34 annd read the 3rd line" it should be changed to $str = "go to page number 35 and read the 4rd line".
I tried using
$str =~ s/[\d]/$&+1/g
but it gives output as string i.e. "go to page number 34+1 and read the 3+1rd line"
Upvotes: 2
Views: 1326
Reputation: 104065
How about this:
$ echo "foo 1 bar 2" | perl -pE 's/(\d+)/$1+1/ge'
foo 2 bar 3
The point is the e
regex option that treats the replacement part as an expression.
Upvotes: 9