Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by choroba (Archbishop)
on Dec 22, 2018 at 23:11 UTC ( #1227621=note: print w/replies, xml ) Need Help??


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

Using two substitutions is cleaner:
$dest =~ s/\W+\[MSRN\]|\W+/_/g; $dest =~ s/_$//;
i.e. replace sequences of non-word characters by underscores, then remove the last underscore.

I can't think of a non-hackish solution with one substitution only.

$dest =~ s/(?:\W+(?:MSRN\])?|\W+)(?(?=$)(?{$%=1})|(?{$%=0}))/$%?'' +:'_'/ge;
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Search and replace regex, but only retain a portion of the string
by ghenry (Vicar) on Dec 23, 2018 at 13:43 UTC

    Of course. For goodness sake, an OR. Thanks a lot! I can live with <code>_</code.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2022-06-25 19:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (83 votes). Check out past polls.

    Notices?