开发者

How do I strip hash tags from a tweet and return them as an array?

开发者 https://www.devze.com 2023-01-27 03:06 出处:网络
I need to strip hashtags from a tweet and return those hash tags as an arr开发者_如何学运维ay.

I need to strip hashtags from a tweet and return those hash tags as an arr开发者_如何学运维ay.

I know this is possible with the proper regex but I can't seem to find the right regex to use.


hashtag_array = tweet.split.find_all{|word| /^#.+/.match word}

Split the string containing the tweet (by default split splits on whitespace). The resulting array contains all the words in the tweet. find_all returns an array with elements in the original array for which the given block returns true. So in the block we check for words beginning with the hash (#).

Documentation on the split method is here, find_all is here.


"#qwe rty#asd #fgh".scan(/(?:^|\s)(#\S+)/).flatten

or

"#qwe rty#asd #fgh".split.grep /^#./
0

精彩评论

暂无评论...
验证码 换一张
取 消