6

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'fromarrays_of_strings_to_check_against不应匹配'dogs'from 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只包含整个单词,而不是多单词字符串,则可以&将两个数组放在一起。如果结果的长度 > 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 回答
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),它是匹配发生的偏移量的索引。

如果你绝对必须有trueor false,你可以这样做:

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
于 2012-05-31T03:06:55.987 回答
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 回答