ghenry has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
It's been a long time, but I know this is the right place to ask. So here goes. I'm looking for a pointer to the right doc section to read in https://perldoc.perl.org/perlre.html to help me with my test code below:
use Modern::Perl; use strict; use warnings; my $dest = q{UK Mobile - Vodafone [GBRVF] [MSRN]}; $dest =~ s/(\W+)\s?(\[MSRN\])?/_/g; say $dest;
which gives me UK_Mobile_Vodafone_GBRVF_MSRN_
I'm trying to turn UK Mobile - Vodafone [GBRVF] [MSRN] into UK_Mobile_Vodafone_GBRVF_, but [MSRN] might not always be there. I want this done all in one substitution too. I think I even want to get to UK_Mobile_Vodafone_GBRVF.
The software I'm doing this in can possibly process the same variable twice, with the second run being a second substitution, so I could make UK Mobile - Vodafone [GBRVF] [MSRN] become UK Mobile - Vodafone [GBRVF], then process it again to become UK_Mobile_Vodafone_GBRVF_
Many thanks,
Gavin.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Search and replace regex, but only retain a portion of the string
by choroba (Cardinal) on Dec 22, 2018 at 23:11 UTC | |
by ghenry (Vicar) on Dec 23, 2018 at 13:43 UTC | |
Re: Search and replace regex, but only retain a portion of the string
by tybalt89 (Monsignor) on Dec 23, 2018 at 01:12 UTC | |
by ghenry (Vicar) on Dec 23, 2018 at 13:43 UTC | |
Re: Search and replace regex, but only retain a portion of the string (updated x4)
by AnomalousMonk (Archbishop) on Dec 22, 2018 at 22:51 UTC | |
by ghenry (Vicar) on Dec 23, 2018 at 13:42 UTC | |
Re: Search and replace regex, but only retain a portion of the string
by kschwab (Vicar) on Dec 23, 2018 at 20:18 UTC |