Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Regex boundary match

by AnomalousMonk (Bishop)
on Feb 08, 2020 at 21:50 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2020-05-25 06:23 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (143 votes). Check out past polls.