Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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

by g man (Initiate)
on Apr 16, 2000 at 19:42 UTC ( #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


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!
  • 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, 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
  • 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (5)
    As of 2018-09-24 05:57 GMT
    Find Nodes?
      Voting Booth?
      Eventually, "covfefe" will come to mean:

      Results (191 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!