Here's an excerpt from chapter 7 (substitution):
Example 7.4 Sorting text by length for use in a regex.
$keys = join '|', # 4 map { quotemeta } # 3 sort { length($b) <=> length($a) } # 2 keys %change; # 1
  1. We get the list of keys from the hash – these are the strings...
  2. We sort the keys by their length, so that the longest strings come first...
  3. We run them through the quotemeta() function to make them safe for regexes...
  4. And then we join the regex-safe strings with "|" in between them, for regex alternation.
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. :)

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


In reply to Re: Re (tilly) 1: Replace all at once by japhy
in thread Replace all at once by Parham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.