Forum Moderators: coopster & phranque

Message Too Old, No Replies

Interesting Regex Problem

         

rfontaine

2:25 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



Is there a way to add numbers in regular expressions?

I have a bunch of text within which are sprinkled with lines that contain this:

top:1032
top:893
top:999
etc.

I would like to add, say, 1589 to each of these numbers, so that

top:1000 becomes top:2589
top:1032 becomes top:2721
etc.

I have gotten roughly this far:
$text = preg_replace(/top:/, ,$text);

Any ideas?
Thank you in advance

bennymack

3:52 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



cat >> test.pl
#!/usr/bin/perl -w
my $foo = 'top:1000';
$foo =~ s#(.*?)\:(.*?)$#sprintf("$1\:%d", 1589+$2)#e;
print $foo."\n";
CTRL+D
./test.pl
top:2589

[edited by: jatar_k at 4:51 pm (utc) on July 26, 2005]
[edit reason] disabled smiles [/edit]

rfontaine

6:08 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



Thank you!