Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Replace multiple strings in a string

by BillKSmith (Chaplain)
on May 07, 2014 at 20:36 UTC ( #1085376=note: print w/ replies, xml ) Need Help??


in reply to Replace multiple strings in a string

Do them all at once.

use strict; use warnings; my %replacement = ( '?a?' => 'aRep', '?b?' => 'bRep', '?c?' => 'cRep', ); my $regex = join '|', keys %replacement; $regex =~ s/\?/\\\?/g; my $string = '?a? ?b? ?c?'; print "old string: $string\n"; $string =~ s/($regex)/$replacement{$1}/ge; print "new string: $string\n";
Bill


Comment on Re: Replace multiple strings in a string
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-10-21 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (106 votes), past polls