Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Regex boundary match

by AnomalousMonk (Archbishop)
on Feb 08, 2020 at 21:50 UTC ( [id://11112645]=note: print w/replies, xml ) Need Help??


in reply to Regex boundary match

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:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found