6

string.include?(other_string)文字列に別の文字列が含まれているかどうかを確認するために使用されます。文字列に文字列の配列から少なくとも 1 つの文字列が含まれているかどうかを確認する良い方法はありますか?

string_1 = "a monkey is an animal. dogs are fun"

arrays_of_strings_to_check_against = ['banana', 'fruit', 'animal', 'dog']

文字列 が含まれているtrueため、これは を返します。から削除すると、 が返されます。string_1'animal''animal'arrays_of_strings_to_check_againstfalse

完全に一致する必要があるため、文字列'dog'fromはfromarrays_of_strings_to_check_againstと一致しないことに注意してください。'dogs'string_1

Rails 3.2.0 と Ruby 1.9.2 を使用しています

4

6 に答える 6

7
arrays_of_strings_to_check_against.map{ |o| string_1 =~ /\b#{Regexp.escape(o)}\b/ }.any?

あるいは:

arrays_of_strings_to_check_against.any?{ |o| string_1 =~ /\b#{Regexp.escape(o)}\b/ }
于 2012-05-31T02:18:41.177 に答える
3

array_of_strings_to_check_against複数単語の文字列ではなく、単語全体のみが含まれている場合は&、2 つの配列を一緒にすることができます。結果の長さが 0 より大きい場合、一致がありました。ただし、 の前に.split(' ')、単語以外のスペース以外の文字を削除する必要があります。そうしないと、この場合、animal.(with .) が配列にないため失敗します。

if (string_1.gsub(/[^\w\s]/).split(' ') & array_of_strings_to_check_against).length > 0
  puts "Match!!"
end

コメント後の更新: 大文字と小文字を区別しないバージョン

if (string_1.downcase.gsub(/[^\w\s]/).split(' ') & array_of_strings_to_check_against).length > 0
  puts "Match!!"
end
于 2012-05-31T02:14:03.767 に答える
2

Regexp.unionこの場合、あなたの友達です。検討:

# the words we're looking for...
target_words = %w[ore sit ad sint est lore]

search_text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'

# define a search ignoring case that looks for partial words...
partial_words_regex = /#{ Regexp.union(target_words).source }/i
partial_words_regex.to_s # => "(?i-mx:ore|sit|ad|sint|est|lore)"

# define a search ignoring case that looks for whole words...
whole_words_regex = /\b(?:#{ Regexp.union(target_words).source })\b/i
whole_words_regex.to_s # => "(?i-mx:\\b(?:ore|sit|ad|sint||lore)\\b)"

# find the first hit...
search_text[whole_words_regex] # => "sit"

# find all partial word hits...
search_text.scan(partial_words_regex) # => ["Lore", "sit", "ad", "ore", "lore", "ad", "lore", "sint", "est"]

# find all whole word hits...
search_text.scan(whole_words_regex) # => ["sit", "ad", "sint", "est"]

すべてを文脈に入れる:

string_1 = "a monkey is an animal. dogs are fun"
arrays_of_strings_to_check_against = ['banana', 'fruit', 'animal', 'dog']
string_1[Regexp.union(arrays_of_strings_to_check_against)] # => "animal"
string_1.scan(Regexp.union(arrays_of_strings_to_check_against)) # => ["animal", "dog"]
于 2012-05-31T05:06:52.863 に答える
0
def check_string
  arrays_of_string_to_check_against.each do |item|
      is_include = string_1.include?(item)
  end
end
于 2012-05-31T02:21:05.600 に答える
0
(string_1.scan(/\w+/) & arrays_of_strings_to_check_against).size > 0
于 2012-06-01T05:27:26.087 に答える