Here's an elaboration on choroba's approach. The idea is to make matching insensitive to case and to variations in embedded whitespace. Note that introducing pattern matching into the substitution keys comes with potential pitfalls, so a robust test suite should probably accompany this solution.

use strict; use warnings; use Data::Dump qw(dd); # for debug # function declarations (just for syntactic sweetening). sub remove_leading_trailing_whitespace; sub normalize_embedded_whitespace; sub normalize_case; # build phrase translation dictionary. my %dict = map @$_, # expand to key/value pairs map { $_->[0] = # do all key normalizations normalize_case normalize_embedded_whitespace remove_leading_trailing_whitespace $_->[0] ; $_; } # keys will be normalized in hash; value strings used as defined. [ 'Animal Source Food' => q{aliment d'origine animale}, ], [ ' balanced diet ' => q{regime alimentaire equilibre}, ], [ ' food ' => q{aliment}, ], [ 'nutrition' => q{diet}, ], [ 'nutrition assessment' => q{evaluation de l'etat nutritionnel}, ], ; # dd \%dict; # FOR DEBUG # regex construction assumes dictionary keys are fully normalized. my ($rx_phrase) = map qr{ (?i) \b (?: $_) \b }xms, join ' | ', map match_any_embedded_whitespace($_), sort { length($b) <=> length($a) } keys %dict ; # print "$rx_phrase \n"; # FOR DEBUG my $text = <<"EOT"; this is a nutrition assessment for animal source food needed for a Balanced \t\t Diet of FOOD for proper nutrition. EOT print "before: [[$text]] \n\n"; $text =~ s{ ($rx_phrase) } {<<$dict{ normalize_embedded_whitespace normalize_case $1 }> +>}xmsg; print "after: [[$text]] \n\n"; exit; # subroutines ###################################################### # s/// and tr/// expressions assume pre-5.14 perl version: no /r # modifier. sub remove_leading_trailing_whitespace { (my $r = $_[0]) =~ s{ \A \s+ | \s+ \z }{}xmsg; # pre-5.14 return $r; } sub normalize_embedded_whitespace { (my $r = $_[0]) =~ tr/ \t\n\r\f/ /s; # pre-5.14 return $r; } sub match_any_embedded_whitespace { (my $r = $_[0]) =~ s{ \s+ }' \s+ 'xmsg; # pre-5.14 return $r; } sub normalize_case { return lc $_[0]; }
Output:
c:\@Work\Perl\monks\Anonymous Monk\1231409>perl dynamic_match_2.pl before: [[this is a nutrition assessment for animal source food needed for a Balanced Diet of FOOD for proper nutrition. ]] after: [[this is a <<evaluation de l'etat nutritionnel>> for <<aliment + d'origine animale>> needed for a <<regime alimentaire equilibre>> of <<aliment>> for proper <<diet>>. ]]
(Tested under Perl version 5.8.9.)


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


In reply to Re: Matching wirth ordered array by AnomalousMonk
in thread Matching wirth ordered array by Anonymous Monk

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.