首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找字符串Ruby中的所有单词“The”

查找字符串Ruby中的所有单词“The”
EN

Stack Overflow用户
提问于 2021-04-01 14:23:14
回答 1查看 90关注 0票数 1

我有这样的绳子

代码语言:javascript
复制
the horse and the hound and the horn that belonged to

如何在字符串中的“the”字之后得到所有单词?所以它就这样回来了

代码语言:javascript
复制
the horse, the hound, the horn
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-01 14:35:59

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

代码语言:javascript
复制
"the horse and the hound and the horn that belonged to".scan(/\bthe \w+/).join(", ")
# "the horse, the hound, the horn"
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66906299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档