Dissasembling tweets in Ruby using Regular Expressions

Here’s a quick way of dissasembling tweets in Ruby, if you were making a twitter-style application. Check this out;

Starting with;

irb >> tweet = "Hello @chicknstu how are you? #boring #ff #followfriday"

How do you get all of the hashtags and mentions out of there? Never underestimate the power of the “scan” method and regular expressions!

irb >> tweet.scan(/#+?\w*/) { |hashtag| puts hashtag }
#boring
#ff
#followfriday
irb >> tweet.scan(/@+?\w*/) { |mention| puts mention }
@chicknstu

Although I am sure the regex hardcore could probably find problems with this, that’s not bad huh? Instead of puts’ing them you could whack them in a database table somewhere. What about displaying the tweet itself and making the hashtags clickable under html? Well, we can use a combination of regular expressions (gsub/sub methods this time) and interpolation to do that in one line. Here’s a start…

irb >> clickableHashtagsTweet = tweet.gsub(/#+?\w*/) { |x| "<a href = 'something.com?tag=#{x.sub(/#/,"")}'>#{x}</a>" }

will create a string with un-titled anchors (title them!!!) which could easily be adapted for mentions too. These are just simple examples and I wouldn’t reccomend you use them un-secured in a production environment as in the above state they are pretty insecure, but a pretty good start I think and a good example of how you can achieve quite a bit of functionality in one line of code using regular expressions.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s