Nice try, but that was not my use case. I only multiplied the int part to prove I could do it from the CODE part of s///. Now let's say that IS my use case, that I want to multiply AND commify WITHOUT grouping, and the real number MUST be in a string, as in hippo's test paradigm, to wit:
$orig = 'This is a real number, 123456.56567'; $want = 'This is a real number, 493,826.26268';
I can do it with mine but not yours, as in:
$want = 'This is a real number, 493,826.26268'; ($have = (($orig) =~ s{ \d+\.?\d* }{ $& * 4 }erx )) =~ s{ \d+ }{ $& =~ s/ (?<=\d) (?= (?:\d{3} )+ (?!\d) ) /,/xrg; }ex; $intpart = $&; is $intpart, 493826, '$& int part captured'; is $have, $want, 'Insert commas where appropriate';
And as the Monks prefer, no grouping () to be seen. That is three s/// in a one-liner. First, grab the real number and factor it; next, grab just the int part and commify it; finally, put it back in the string. Man, s/PATTERN/CODE/e is potent mojo.
In reply to Re^6: regex in REPLACEMENT in s///
by perlboy_emeritus
in thread regex in REPLACEMENT in s///
by perlboy_emeritus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |