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

Re: tr///

by japhy (Canon)
on Jan 24, 2002 at 21:42 UTC ( #141256=note: print w/ replies, xml ) Need Help??

in reply to Replacing multiple words in a regex

tr/// is strictly for character-to-character transliteration. You want substitution.

%change = qw( green yellow blue red ); my $rx = join '|', # alternation map { quotemeta } # make it a safe string sort { length($b) <=> length($a) } # so that 'broken' matches before 'broke' keys %change; $str =~ s/($rx)/$change{$1}/g;
This is a common idiom.

Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Comment on Re: tr///
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (17)
As of 2015-11-30 17:26 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (777 votes), past polls