Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How do I match for several strings, matching the longest stringfirst?

( #7769=categorized question: print w/ replies, xml ) Need Help??
Contributed by g man on Apr 16, 2000 at 19:42 UTC
Q&A  > regular expressions


Description:

The following is a excerpt of text file:
right lymph node lymph fluid
how would i match for the longest string first, then shorter string in the example of above i would want my program to print out lymph node as the tissue type in line 1 but lymph in line 2 i have a table of relevant terms to match to, but there are separate entries for lymph and lymph node

Answer: How do I match for several strings, matching the longest string first?
contributed by btrott

I think a good solution would be to sort the terms that you're matching for and create a regexp of the strings in sorted order. Sort them so that the regexp tries to match the longest string first, then moves on down in length until it's trying to match the shortest one.

Something like the following should work:

my @terms = ('lymph', 'lymph node'); my @text = ('right lymph node', 'lymph fluid'); # create a regexp that will match the longest # string first and capture the string that matched my $words = '\b(' . join('|', sort { length $b <=> length $a } @terms) . ')\b'; for my $text (@text) { if ($text =~ /$words/) { print $text, ": matched => ", $1, "\n"; } }
Answer: How do I match for several strings, matching the longest string first?
contributed by chromatic

Another option is to arrange your search terms into a sorted list:

my @terms = sort { length $b <=> length $a } ('lymph', 'lymph node'); my @text = ('right lymph node', 'lymph fluid'); my %results; foreach my $term (@terms) { $results{$term} = (grep /\b$term\b/, @text); # find matches @text = grep !/\b$term\b/, @text; # remove matches } foreach (keys %results) { print "$_:\t", $results{$_}, "\n"; }
This is likely less expensive with more search terms than building a large regexp, but the grep unfound operation may not help.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (14)
    As of 2014-08-27 18:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (248 votes), past polls