tags:

views:

67

answers:

2

I have a string in the following format: \main\stream\foo.h\3

it may have more or less "sections", but will always end with a slash followed by an integer. Other examples include:

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

\main\foo.c\9

I need to, in perl, increment the number at the end of the string and leave the rest alone. I found an example on this site that does exactly what I want to do (see http://stackoverflow.com/questions/1742798/increment-a-number-in-a-string-in-with-regex) only the language is javascript. The solution given was: .replace(/\d+$/,function(n) { return ++n })

I need to do the same thing in perl.

Thanks.

+8  A: 

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

Something like:

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

should work.

friedo
worked like a charm! Thanks.
Too bad auto-increment is limited to strings that match `/^[a-zA-Z]*[0-9]*\z/`
Greg Bacon
+1  A: 

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

James O'Sullivan