I need to strip hashtags from a tweet and return those hash tags as an array.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With