Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: hashes in regexes

by merlyn (Sage)
on Mar 29, 2001 at 01:26 UTC ( #67959=note: print w/replies, xml ) Need Help??

in reply to hashes in regexes

untested, but I often get this stuff right on the first try: {grin}
my $regex = join "|", map quotemeta, keys %substitute_hash; $regex = qr/$regex/; # compile ... $string =~ s/($regex)/$substitute_hash{$1}/g;

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re (tilly) 2: hashes in regexes
by tilly (Archbishop) on Mar 29, 2001 at 19:32 UTC
    You might want to throw in a reverse sort there. It was not in the spec, but it may happen that someone will want to do one substitution on "foo" and another on "foobar". Since Perl's REs are DFAs rather than NFAs this is not going to work unless in the RE you see foobar before foo.,,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://67959]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2018-04-22 07:45 GMT
Find Nodes?
    Voting Booth?