有没有一种很好的方法来检查一个字符串是否包含一个字符串数组中的至少一个字符串?

时间:2012-05-31 02:09:52

标签: ruby-on-rails ruby ruby-on-rails-3 string

string.include?(other_string)用于检查字符串是否包含另一个字符串。有没有一种很好的方法来检查一个字符串是否包含一个字符串数组中的至少一个字符串?

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_against,则会返回false

请注意,'dog'中的字符arrays_of_strings_to_check_against'dogs'中的string_1不匹配,因为它必须完全匹配。

我正在使用Rails 3.2.0和Ruby 1.9.2

6 个答案:

答案 0 :(得分: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/ }

答案 1 :(得分:4)

如果array_of_strings_to_check_against只包含整个单词而不包含多字符串,则可以&将两个数组放在一起。如果结果的长度> 0,有一场比赛。但是,在.split(' ')之前,您必须删除非单词,非空格字符。否则,在这种情况下,它会失败,因为animal..}不在您的数组中。

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

答案 2 :(得分:3)

str1  = "a monkey is an animal. dogs are fun"
str2  = "a monkey is a primate. dogs are fun"
words = %w[banana fruit animal dog]
word_test = /\b(?:#{ words.map{|w| Regexp.escape(w) }.join("|") })\b/i

p str1 =~ word_test,  #=> 15
  str2 =~ word_test   #=> nil

如果你得到nil则没有匹配;否则你会得到一个整数(你可以像true那样对待),它是匹配发生的偏移量的索引。

如果您必须拥有truefalse,则可以执行以下操作:

any_match = !!(str =~ word_test)

插值创建的正则表达式是:

/\b(?:banana|fruit|animal|dog)\b/i

... \b匹配“字边界”,从而阻止dog中的dogs匹配。

修改:上面的答案不再使用Regexp.union,因为这会创建区分大小写的正则表达式,而问题需要不区分大小写。

或者,我们可以在测试之前将所有内容强制为小写以获得不区分大小写:

words = %w[baNanA Fruit ANIMAL dog]
word_test = /\b#{ Regexp.union(words.map(&:downcase)) }\b/
p str1.downcase =~ word_test,
  str2.downcase =~ word_test

答案 3 :(得分: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"]

答案 4 :(得分:0)

def check_string
  arrays_of_string_to_check_against.each do |item|
      is_include = string_1.include?(item)
  end
end

答案 5 :(得分:0)

(string_1.scan(/\w+/) & arrays_of_strings_to_check_against).size > 0