Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: reg. expr. multiple substitutions

by dulwar (Scribe)
on May 17, 2012 at 16:53 UTC ( #971117=note: print w/ replies, xml ) Need Help??


in reply to reg. expr. multiple substitutions

Use a hash:

my %subs = ( tag1 => 'subst1', tag2 => 'subst2', ... ); my $pattern = join('|', keys %subs); $str =~ s/($pattern)/$subs{$1}/g;

It would be a good idea to simplify $pattern if there is some common pattern to the tags.

By the way, you need to use the g modifier to replace all occurrences of each tag if it can occur more than once in the string, also if you are substituting each tag separately.


Comment on Re: reg. expr. multiple substitutions
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2015-07-08 04:29 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 (94 votes), past polls