Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Replace multiple strings in a string

by BillKSmith (Deacon)
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 surveying the Monastery: (7)
As of 2015-07-05 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls