如何从 Ruby 字符串中抓取文本片段?

发布于 2024-12-25 06:55:41 字数 725 浏览 1 评论 0原文

如果用户提交如下字符串:

我的客厅计划#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神仙妹妹 2025-01-01 06:55:42
input = "My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!"
text = input.match('^(.*?)#')[1]
tags = input.scan(/#(.*?) /)
people = input.scan(/@(.*?) /)
description = input.match('-(.*?)
)[1]
input = "My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!"
text = input.match('^(.*?)#')[1]
tags = input.scan(/#(.*?) /)
people = input.scan(/@(.*?) /)
description = input.match('-(.*?)
)[1]
两人的回忆 2025-01-01 06:55:42
str = 'My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!'

text = str[/^([^#\@]+)/, 1].strip # => "My living room plans"
str.sub!(text, '') # => " #plans #livingroom @cbmeeks #design @moe @larry -this is cool!"

tags        = str.scan( /#([a-z0-9]+)/ ).flatten # => ["plans", "livingroom", "design"]
people      = str.scan( /@([a-z0-9]+)/ ).flatten # => ["cbmeeks", "moe", "larry"]
description = str.scan( /-(.+)/        ).flatten # => ["this is cool!"]
str = 'My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!'

text = str[/^([^#\@]+)/, 1].strip # => "My living room plans"
str.sub!(text, '') # => " #plans #livingroom @cbmeeks #design @moe @larry -this is cool!"

tags        = str.scan( /#([a-z0-9]+)/ ).flatten # => ["plans", "livingroom", "design"]
people      = str.scan( /@([a-z0-9]+)/ ).flatten # => ["cbmeeks", "moe", "larry"]
description = str.scan( /-(.+)/        ).flatten # => ["this is cool!"]
弃爱 2025-01-01 06:55:41

这将自动删除 #@-,并以任意顺序匹配:

string = "My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!"
text = string[/^(.*?)+(?=\s[@#-])/]
tags = string.scan /(?<=#)\w+/
people = string.scan /(?<=@)\w+/
description = string[/(?<=-)(.*?)+?(?=($|\s[@#]))/]

This will automatically remove the #, @, -, and will match in any order:

string = "My living room plans #plans #livingroom @cbmeeks #design @moe @larry -this is cool!"
text = string[/^(.*?)+(?=\s[@#-])/]
tags = string.scan /(?<=#)\w+/
people = string.scan /(?<=@)\w+/
description = string[/(?<=-)(.*?)+?(?=($|\s[@#]))/]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文