Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Combining two actions in RegExp

by jwkrahn (Abbot)
on Jan 23, 2018 at 19:43 UTC ( [id://1207773]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1207773]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-18 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found