I wondered how you might cope with dictionary words with a space in them, like "bar graph". The solution I came up with was to do a descending lexical sort of the dictionary keys when constructing the regex. That way "bar graph" will be matched in preference to "bar" and "graph" will already have been consumed by the regex if preceded by "bar". This code

use strict; use warnings; use Data::Dumper; open my $dictFH, q{<}, \ <<'DICT' or die qq{open: < HEREDOC: $!\n}; 1 eat 2 habit 3 boy 4 man-kind 5 man 6 kind 7 bar 8 graph 9 bar graph DICT my %dict = reverse map { split m{\s+}, $_, 2 } map { chomp; $_ } <$dictFH>; close $dictFH or die qq{close: < HEREDOC: $!\n}; print Data::Dumper->Dumpxs( [ \ %dict ], [ qw{ *dict } ] ); my $text = <<'TEXT'; The boy has a habit of eating kind of like a man. Man-kind, as a group, does not wear habits. Kind of you to watch what you eat. Make a bar graph of what drinks are drunk in a bar and the graph could be coloured in. TEXT my $rxDict = do { local $" = q{|}; qr {(?xi) \b( @{ [ map quotemeta, sort { $b cmp $a } keys %dict ] } )\b } }; $text =~ s{$rxDict}{ $dict{ lc $1 } }eg; print $text;

produces

%dict = ( 'eat' => '1', 'man' => '5', 'kind' => '6', 'bar' => '7', 'man-kind' => '4', 'bar graph' => '9', 'boy' => '3', 'graph' => '8', 'habit' => '2' ); The 3 has a 2 of eating 6 of like a 5. 4, as a group, does not wear habits. 6 of you to watch what you 1. Make a 9 of what drinks are drunk in a 7 and the 8 could be coloured in.

I hope this is of interest.

Cheers,

JohnGG

Update: Sorting the keys in ascending lexical order shows what happens when the shorter is chosen before the longer. The transformed text becomes

The 3 has a 2 of eating 6 of like a 5. 5-6, as a group, does not wear habits. 6 of you to watch what you 1. Make a 7 8 of what drinks are drunk in a 7 and the 8 could be coloured in.

In reply to Re: seaching for replacement by johngg
in thread seaching for replacement 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.