in reply to Combining two actions in RegExp

By using tr/// instead of a regular expression:

$ perl -le' my $phone = "(xxx)1234-0000"; $phone =~ tr/)(/-/d; print $phone; ' xxx-1234-0000

Replies are listed 'Best First'.
Re^2: Combining two actions in RegExp
by Anonymous Monk on Jan 23, 2018 at 20:01 UTC
    That works, can spaces be removed from the string if found using TR?
    e.g. my $phone = "(xxx) 1234-0000";
      $phone =~ tr/)(/-/d; says to delete the parentheses and also replace the closing paren with '-';

      To also delete any spaces, you could use $phone =~ tr/)(\x20/-/d; where '\x20' is the hex value of a 'space'. You could also use $phone =~ tr/)( /-/d; without the hex value of a space and just use a literal space instead. (The hex version just makes the 'space' more explicit).

        Hi, in a situation where the string looks like this one:

         my $phone = "(777) 1234-0000 RAM:007";

        How would remove just the space after the "(777) "?
        The end result would be like:  777-1234-0000 RAM:007

        Thank you!