From: Jens Wille Date: 2008-04-30T23:18:16+09:00 Subject: Re: Search string for occurneces of words stored in array Phillip Gawlowski [2008-04-30 16:09]: > John Butler wrote: > | Hi, > | > | I have a sentence "This is my test sentence" and an array["is", "the", > | "my"] and what i need to do is find the occurence of any of thearray > | words in the sentence. > | > | I have this working in a loop but i was wondering is there a way to do > | it using one of rubys string methods. > | > | Its sililar to the include method but searching for multiple words not > | just one. > | > | "This is my test sentence".include?("This") returns true > | > | but i want something like > | > | "This is my test sentence".include?("This", "is", "my") > | > | anyone got a nice way to do this? I only need to find if one of the > | words occure and then i exit. > | > | JB > > How about '["is", "the", "my"].each'? > > I.e.: > > ["is", "the", "my"].each do |word| > ~ break if "the test sentence'.include? word > end i'd prefer Enumerable#any?: sentence, words = "This is my test sentence", ["This", "is", "my"] words.any? { |word| sentence.include?(word) } or Regexp: sentence =~ Regexp.union(*words) cheers jens -- Jens Wille, Dipl.-Bibl. (FH) prometheus - Das verteilte digitale Bildarchiv f端r Forschung & Lehre Kunsthistorisches Institut der Universit辰t zu K旦ln Albertus-Magnus-Platz, D-50923 K旦ln Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de http://www.prometheus-bildarchiv.de/