在字符串Ruby中查找单词“The”之后的所有单词

我有这样的字符串

the horse and the hound and the horn that belonged to

如何获取字符串中“the”字后的所有字词?所以它像这样返回

the horse, the hound, the horn

回答

您可以使用String#scan来获取每个“the”加上一个空格,后跟任何单词,然后join是这些结果:

"the horse and the hound and the horn that belonged to".scan(/bthe w+/).join(", ")
# "the horse, the hound, the horn"

  • Don't forget to put a `b` before `the` in the regexp. `"breathe strongly and keep the pace"`

以上是在字符串Ruby中查找单词“The”之后的所有单词的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>