我不确定为什么我的正则表达式有时会被忽略。我有时说,因为当用其他句子测试它时,例如“不要不要我”,它似乎保持单引号标记为希望。
def alternate_words(words)
words.split.map {|x| x.gsub(/[^\‘0-9A-Za-z]/, "")}
.delete_if {|x| x == ""}
.select.with_index {|x, i| i.even?}
end
alternate_words("Would a test upon . . . ? I hope not. But even if it is, don’t let that mail if you ! ?")
#=> ["Would", "test", "I", "not", "even", "it", "dont"]
然而,这是一个测试句,所以它无法通过,因为它不断返回“不要”而不是“不要”
答案 0 :(得分:0)
看起来你的正则表达式需要卷曲和直撇号:
x.gsub(/[^\‘\’'0-9A-Za-z]/, "")