NB: If the word | substring translations are essentially one-to-one, you can use the technique discussed in haukex's Building Regex Alternations Dynamically article to do a fairly fast search/replace:

c:\@Work\Perl\monks>perl use strict; use warnings; use Test::More 'no_plan'; use Test::NoWarnings; my %replace = qw( milk white toast brown cheese yellow peas green ); my ($rx_search) = map qr{ (?i) (?<! [-\w]) (?: $_) (?! [-\w]) }xms, join ' | ', map quotemeta, reverse sort keys %replace ; print $rx_search, "\n"; # for debug VECTOR: for my $ar_vector ( 'no changes in these', [ 'milky appease peasoup cheese-toast' => 'milky appease peasoup cheese-toast' ], 'parts of all these should change', [ 'mIlK, some PeAs, cheese and toast.' => 'white, some green, yellow and brown.' ], ) { if (not ref $ar_vector) { note $ar_vector; next VECTOR; } my ($string, $expected) = @$ar_vector; (my $replaced = $string) =~ s{ ($rx_search) }{$replace{lc $1}}xmsg +; is $replaced, $expected, "'$string' -> '$expected'"; } done_testing; exit; __END__ (?msx-i: (?i) (?<! [-\w]) (?: toast | peas | milk | cheese) (?! [-\w]) + ) # no changes in these ok 1 - 'milky appease peasoup cheese-toast' -> 'milky appease peasoup +cheese-toast' # parts of all these should change ok 2 - 'mIlK, some PeAs, cheese and toast.' -> 'white, some green, yel +low and brown.' 1..2 ok 3 - no warnings 1..3


Give a man a fish:  <%-{-{-{-<


In reply to Re: Regex boundary match by AnomalousMonk
in thread Regex boundary match by ultranerds

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.