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