in reply to Re: Doing multiple search and replace operations on a string
in thread Doing multiple search and replace operations on a string

The (\w) will match and capture a digit which doesn't exist in the hash so will be lost in the substitution. Without a look-behind you will also consume and lose the digit before. Better I think to use a character class along with the look-behind.

$ perl -E ' $val = q{2.73M}; %exp = ( M => 6 ); $val =~ s/\d(\w)/exists $exp{ $1 } ? q{e} . $exp{ $1 } : q{} /e; say $val;' 2.M $ perl -E ' $val = q{2.73M}; %exp = ( M => 6 ); $val =~ s/(?<=\d)(\w)/exists $exp{ $1 } ? q{e} . $exp{ $1 } : q{} /e; say $val;' 2.7M $ perl -E ' $val = q{2.73M}; %exp = ( M => 6 ); $val =~ s/(?<=\d)([TGMkmunp])/exists $exp{ $1 } ? q{e} . $exp{ $1 } : +q{} /e; say $val;' 2.73e6 ~ $

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^3: Doing multiple search and replace operations on a string
by letssee (Initiate) on Feb 21, 2014 at 04:21 UTC
    Thanks for the suggestion. I will try this out.