Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: hashes in regexes

by japhy (Canon)
on Mar 29, 2001 at 01:24 UTC ( #67958=note: print w/ replies, xml ) Need Help??

in reply to hashes in regexes

Your methods goes through the string N times, where N is the number of keys in your hash. Create the regex with qr//:

$keys = join '|', map "\Q$_\E", keys %sub_hash; $keys_REx = qr/$keys/; $string =~ s/($keys_REx)/$sub_hash{$1}/g;

japhy -- Perl and Regex Hacker

Comment on Re: hashes in regexes
Download Code
Replies are listed 'Best First'.
Re^2: hashes in regexes
by tadman (Prior) on Mar 29, 2001 at 04:12 UTC
    I'm not sure about the application, but it might be advisable to at least put in some '\b's to prevent unfortunate collisions that would render some names as "Mikestone", or "Moeseph".

    Here is a slight modification of japhy's (or merlyn's, you decide) code:
    $keys = join '|', map "\\b\Q$_\E\\b", keys %sub_hash; $keys_REx = qr/$keys/; $string =~ s/($keys_REx)/$sub_hash{$1}/g;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2015-10-10 00:44 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (254 votes), past polls