http://www.perlmonks.org?node_id=1227618

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.