Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by btrott (Parson)
on Apr 16, 2000 at 22:14 UTC ( #7770=categorized answer: print w/replies, xml ) Need Help??

Q&A > regular expressions > How do I match for several strings, matching the longest stringfirst? - Answer 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"; } }

  • Comment on Answer: How do I match for several strings, matching the longest string first?
  • Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2017-10-17 17:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (235 votes). Check out past polls.

    Notices?