in reply to Re^5: regex in REPLACEMENT in s///
in thread regex in REPLACEMENT in s///
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: regex in REPLACEMENT in s///
by hippo (Archbishop) on Sep 15, 2023 at 09:08 UTC | |
by perlboy_emeritus (Scribe) on Sep 15, 2023 at 16:35 UTC |