in reply to Re (tilly) 1: Replace all at once
in thread Replace all at once
Example 7.4 Sorting text by length for use in a regex.Doing a benchmark, I see that the reverse sort LIST method is (as one would imagine) faster than the sort { length($b) <=> length($a) } LIST method (by more than two times). I'll incorporate your code, tilly, if'n you don't mind. :)$keys = join '|', # 4 map { quotemeta } # 3 sort { length($b) <=> length($a) } # 2 keys %change; # 1
- We get the list of keys from the hash – these are the strings...
- We sort the keys by their length, so that the longest strings come first...
- We run them through the quotemeta() function to make them safe for regexes...
- And then we join the regex-safe strings with "|" in between them, for regex alternation.
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re (tilly) 1: Replace all at once
by dragonchild (Archbishop) on Dec 10, 2001 at 19:26 UTC | |
by japhy (Canon) on Dec 10, 2001 at 19:39 UTC |