use strict; use 5.020; # for best efficiency on $& use Test::More tests => 11; my $orig = 'This is a real number, 123456.56'; my $want = 'This is a real number, 493824.56'; (my $have = $orig) =~ s/\d+/sprintf "%i", 4 * $&/e; my $intpart = $&; is $intpart, 123456, 'int part captured'; is $have, $want, 'Multiply int part by 4'; ($have = $intpart) =~ s/./3/g; is $have, 333333, 'Int part digits set to "3" trivially, with /./'; ($have = $intpart) =~ s/\d/3/g; is $have, 333333, 'Int part digits set to "3" trivially, with /\\d/'; $want = 'This is a real number, 333333.56'; ($have = $orig) =~ s/\d+/(my $int = $&) =~ s#.#3#g; $int/e; is $have, $want, 'Replace int part in string with all 3s'; $want = 'This is a real number, 123,456.56'; ($have = $orig) =~ s{ \d+ }{ $& =~ s/ (?<=\d) (?= (?:\d{3} )+ (?!\d) ) /,/xrg; }ex; $intpart = $&; is $intpart, 123456, '$& int part captured'; is $have, $want, 'Insert commas where appropriate'; ($have = $orig) =~ s{ (\d+) }{ $1 =~ s/ (?<=\d) (?= (?:\d{3} )+ (?!\d) ) /,/xrg; }ex; $intpart = $1; is $intpart, 123456, '$1 int part captured'; is $have, $want, 'Insert commas where appropriate'; ($have = $orig) =~ s{ (? (\d+)) }{ $+{int} =~ s/ (?<=\d) (?= (?:\d{3} )+ (?!\d) ) /,/xrg; }ex; $intpart = $+{int}; is $intpart, 123456, ' part captured'; is $have, $want, 'Insert commas where appropriate'; exit(0); __END__