Reputation: 11420
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
Upvotes: 2
Views: 105
Reputation: 160621
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!"]
Upvotes: 0
Reputation: 124469
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[@#]))/]
Upvotes: 4
Reputation: 46985
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]
Upvotes: 1