【发布时间】:2020-08-26 03:55:41
【问题描述】:
我想知道一个字符串是否包含子字符串。
字符串示例:
"Hey boy, the cat is in the kitchen ?"
"I want to know if the dog is in the kitchen"
子字符串:
"the ... is in the kitchen"
但是我需要忽略“猫,狗”这两个词,我该怎么做呢?
string.include? 不是正确的方式,因为我想要以“the”开头的完整句子。
【问题讨论】:
-
你可以使用
r = /\bthe +\w+ +is in the kitchen\b/。然后"the cat is in the kitchen".match?(r) #=> true但"the dog is in the kitchenette".match?(r) #=> false。 -
您的问题没有明确提出。
...是什么意思?必须出现“猫”或“狗”的位置?为什么include?不够?它会告诉您子字符串是否出现,仅此而已。为什么开头需要“the”?您是否尝试使用正则表达式?