Hello ScarletRoxanne,

Here’s an approach which replaces each phrase/term with a temporary marker, then removes stopwords, then replaces the markers with their original terms:

use strict; use warnings; use Const::Fast; use Data::Dump; const my $DELIM => '\034'; my %stops = map { lc $_ => 1 } qw( I am the of and you are ); my @terms = ('manager of sales', 'chairman of the board'); @terms = sort { length $b <=> length $a } @terms; # longest firs +t my $file3 = 'I am the Senior Manager of Sales and of Marketing. ' . 'You are the Chairman of the Board of Directors.'; $file3 =~ tr/A-Z/a-z/; # convert to lower case # replace terms with temporary markers $file3 =~ s{$terms[$_]}{$DELIM$_$DELIM}gi for 0 .. $#terms; my @file3 = split /\s+/, $file3; @file3 = grep { ! exists $stops{$_} } @file3; for my $entry (@file3) { if ($entry =~ /\Q$DELIM\E(\d+)\Q$DELIM\E/) { $entry = '*' . $terms[$1] . '*'; } else { $entry =~ s{[[:punct:]]}{}g; # remove punctuation } } print "$_\n" for @file3;

Output:

17:35 >perl 1997_SoPW.pl senior *manager of sales* marketing *chairman of the board* directors 17:35 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Match strings in order of character length, and remove the string from further processing by Athanasius
in thread Match strings in order of character length, and remove the string from further processing by ScarletRoxanne

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.