Perl integer increment in perl

I have a line in the following format:

\main\stream\foo.h\3 

it can have more or less โ€œsectionsโ€, but will always end with a slash followed by an integer. Other examples:

 \main\stream2309\stream222\foo.c\45 \main\foo.c\9 

I need to increase the number at the end of the line in Perl and leave the rest alone. I found an example on this site that does exactly what I want to do (see Increasing the number in a string with a regex ), only in Javascript. The solution is given:

 .replace(/\d+$/,function(n) { return ++n }) 

I need to do the same in Perl.

+6
perl
source share
2 answers

You can use the /e regex modifier to put executable code in your replacement string.

Something like:

 $string =~ s/(\d+)$/$1 + 1/e; 

must work.

+10
source share

Try $var =~ s/(\d+$)/($1 + 1)/e

+3
source share

All Articles