Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何匹配数组中的字符串,而不考虑Ruby中的字符串大小_Arrays_Ruby_String_Loops - Fatal编程技术网

Arrays 如何匹配数组中的字符串,而不考虑Ruby中的字符串大小

Arrays 如何匹配数组中的字符串,而不考虑Ruby中的字符串大小,arrays,ruby,string,loops,Arrays,Ruby,String,Loops,我试图弄清楚以下几点: 当我使用Ruby在终端中运行此命令时,数组中的字符串将被删除,直到我继续键入saxphone_部分数组中的字符串时才完成。但我仍然希望在键入“alto Saxphone 1”时能够从数组中删除该字符串,因为在输入字符串中可以找到“alto 1” 当数组中的字符串匹配时,无论输入字符串的大小如何,如何执行此操作 saxophone_section = ["alto 1", "alto 2", "tenor 1", "tenor 2", "bari sax"] until

我试图弄清楚以下几点:

当我使用Ruby在终端中运行此命令时,数组中的字符串将被删除,直到我继续键入
saxphone_部分
数组中的字符串时才完成。但我仍然希望在键入“alto Saxphone 1”时能够从数组中删除该字符串,因为在输入字符串中可以找到“alto 1”

当数组中的字符串匹配时,无论输入字符串的大小如何,如何执行此操作

saxophone_section = ["alto 1", "alto 2", "tenor 1", "tenor 2", "bari sax"]

until saxophone_section == []

puts "Think of sax parts in a jazz big band."
print ">>"

sax_part = gets.chomp.downcase


# this is the part that is confusing me.  Trying to figure out the method in which 
# a string in the above array matches an input, whether "alto 1" or "alto saxophone 1" 
# or "Eb alto saxophone 1" is typed in ("alto 1" is found in all).  
# How can I make it true in all three (or more) cases?

saxophone_section.any?(sax_part)

# I am thinking that this bottom parts one could be used? or not?
parts = saxophone_section.map {|sax| sax.gsub(/\s+/m, ' ').strip.split(" ")}


#this is the loop to delete the item in the array:

if saxophone_section.include?(sax_part) == true

p saxophone_section.delete_if{ |s| s == sax_part}
puts "Damn, you're lucky"
else
  puts "Woops! Try again."
end
end
puts "You got all parts."


可以选择将字符串转换为数组并进行交集操作。我知道,这不是最好的解决方案,但可能会挽救你的一天

[17] pry(main)> x = "alto saxophone 1"
=> "alto saxophone 1"
[18] pry(main)> y = "i am not an anglophone"
=> "i am not an anglophone"
[19] pry(main)> z = "alto 1"
=> "alto 1"
[20] pry(main)> x.split & z.split == z.split # & is array intersection
=> true
[21] pry(main)> x.split & y.split == y.split
=> false

您应该使用正则表达式来匹配输入。因此,与其创建字符串数组,不如尝试这样的正则表达式数组

saxphone_section=[/alto\s(?:..\s)?1/,/alto\s(?:..\s)?2/,/tenor\s(?:...\s)?1/,/tenor\s(?:..\s)?2/,/bari\s(?:..\s)?sax/]
然后对输入使用与数组中所有元素的匹配,以查找是否与输入字符串匹配

sax\u part=gets.chomp.downcase
index=saxphone_section.find_index{| regex | sax_part.match(regex)}
稍后,如果元素不是nil,您可以使用该索引从数组中删除该元素

萨克斯管部分。删除(索引) 或者您可以使用
Array#delete_,如果
方法直接从数组中删除元素,就像这样

saxphone_section.delete_如果{| regex | sax_part.match(regex)}

注意:您可以使用它来测试正则表达式。

我从这里开始这类任务;这些都是web或应用程序中人机界面的重要构建块:

require 'regexp_trie'

saxophone_section = ["alto 1", "alto 2", "tenor 1", "tenor 2", "bari sax"]
RegexpTrie.union saxophone_section   # => /(?:alto\ [12]|tenor\ [12]|bari\ sax)/
RegexpTrie.union
的输出模式将匹配
saxphone\u部分中的所有字符串。该模式简洁高效,最重要的是,不必手动生成

将该模式应用于正在创建的字符串将显示当存在匹配时是否有命中,但仅当有足够的字符串匹配时

这就是常规Trie非常有用的地方。当您试图找到可能的命中率时,在进行完整匹配之前,Trie可以找到所有的可能性:

require 'trie'

trie = Trie.new
saxophone_section = ["alto 1", "alto 2", "tenor 1", "tenor 2", "bari sax"]

saxophone_section.each { |w| trie.add(w) }
trie.children('a') # => ["alto 1", "alto 2"]
trie.children('alto') # => ["alto 1", "alto 2"]
trie.children('alto 2') # => ["alto 2"]
trie.children('bari') # => ["bari sax"]

把它们混合在一起,看看你能想到什么。

这是红宝石吗?如果是,请添加“查看”。当尝试浏览多个单词时,匹配模式是有帮助的,但是手工创建模式可能会很痛苦。使用代码创建模式的并集可以产生更加简洁的代码。