The increment of integers at certain positions in perl

I have a line like this:

1,2,4 0:5 1:10 3:14

which i want to convert to

1,2,4 1:5 2:10 4:14

Only numbers up to ":" should be increased by 1.

I tried:

perl -w -e '$s="1,2,4 0:5 1:10 3:14"; 
$s =~ s/([0-9]*):/print(($1+1).":")/ge; 
print("$s\n");'

which strange returns

1:2:4:1,2,4 15 110 114

Is there an easy way to achieve my goal?

+4
source share
1 answer

You were close enough, but should correspond to at least one digit, and then :, and part of the substitution should return the desired result, and not print it.

my $s = "1,2,4 0:5 1:10 3:14"; 
$s =~ s/([0-9]+) (?=:)/ $1+1 /xge; 
print $s, "\n";
+11
source

Source: https://habr.com/ru/post/1598364/


All Articles