如何从 Ruby 字符串中抓取文本片段?
如果用户提交如下字符串:
我的客厅计划#plans #livingroom @cbmeeks #design @moe @larry - 这太酷了!
我想要以下数组/字符串:
text = "My living room plans"
tags = ['plans', 'livingroom', 'design']
people = ['cbmeeks', 'moe', 'larry']
description = "this is cool!"
提交的每个字符串都以 text
片段开头。没有 @
、-
等。我不必担心用户以标签或人开头。细分应该类似于这样,可以按任何顺序排列,除了 TEXT 始终位于第一位。
TEXT [-description] [#tags] [@people]
编辑 我似乎不知道如何正确地抓住它们。例如:
a = "My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!"
/#\w+/.match(a).to_a
#=> ["#plans"] -- only grabs first one
If a user submits a string like:
My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!
I want to have the following arrays/strings:
text = "My living room plans"
tags = ['plans', 'livingroom', 'design']
people = ['cbmeeks', 'moe', 'larry']
description = "this is cool!"
Every string submitted will start with the text
piece. No @
, -
, etc. I don't have to worry about a user starting with a tag or person. The breakdown should look something like this, in any order except TEXT is always first.
TEXT [-description] [#tags] [@people]
EDIT
I can't seem to figure out how to grab them correctly. For example:
a = "My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!"
/#\w+/.match(a).to_a
#=> ["#plans"] -- only grabs first one
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将自动删除
#
、@
、-
,并以任意顺序匹配:This will automatically remove the
#
,@
,-
, and will match in any order: