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

时间:2012-01-07 05:01:42

标签: ruby string text-parsing

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

  

我的客厅计划#plans #livingroom @cbmeeks #design @moe @larry -this很酷!

我想拥有以下数组/字符串:

text = "My living room plans"
tags = ['plans', 'livingroom', 'design']
people = ['cbmeeks', 'moe', 'larry']
description = "this is cool!"

提交的每个字符串都将以text片段开头。没有@-等。我不必担心用户以标签或人物开头。除了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

3 个答案:

答案 0 :(得分:4)

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

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[@#]))/]

答案 1 :(得分: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]

答案 2 :(得分:0)

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!"]