Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Search and replace regex, but only retain a portion of the string

by kschwab (Vicar)
on Dec 23, 2018 at 20:18 UTC ( [id://1227644]=note: print w/replies, xml ) Need Help??


in reply to Search and replace regex, but only retain a portion of the string

If the rules are "change any sequence of characters that isn't a-zA-Z into a single underscore", you might like tr// better than a regex:
$ perl my $s="UK Mobile - Vodafone [GBRVF] [MSRN]"; $s=~tr/a-zA-Z/_/cs; print "$s\n"; ^D UK_Mobile_Vodafone_GBRVF_MSRN_

Still needs the second pass to get rid of MSRN_, but clean and straightforward otherwise.

Edit: I see someone already updated another comment node to suggest this, apologies for the dupe.

  • Comment on Re: Search and replace regex, but only retain a portion of the string
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 04:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found