簡體   English   中英

有沒有一種很好的方法來檢查一個字符串是否包含一個字符串數組中的至少一個字符串?

[英]Is there a nice way to check if a string contains at least one string from an array of strings?

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' 如果我們從arrays_of_strings_to_check_against刪除'animal' ,它將返回false

請注意,字符串'dog'arrays_of_strings_to_check_against不應與'dogs'string_1 ,因為它是一個完整的比賽。

我正在使用Rails 3.2.0和Ruby 1.9.2

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/ }

如果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
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匹配“單詞邊界”,從而防止dogdogs匹配。

編輯 :上面的答案不再使用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

在這種情況下, 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"]
def check_string
  arrays_of_string_to_check_against.each do |item|
      is_include = string_1.include?(item)
  end
end
(string_1.scan(/\w+/) & arrays_of_strings_to_check_against).size > 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM