Sunday, May 24, 2009

Regular expression Ruby

Hi Guys,

This post is about my stint on regular expressions using Ruby, programming language. Ruby is one of the powerful languages that provide excellent support to Regular expressions.

There are several method available for String class for checking the patterns. I am gonna write about few of the methods available and also that I know.

Now let’s talk about how can ensure that email is in right format. so let’s take an email id barrack.obama@gmail.com.
Now our aim is to find the word before and after the symbol @. Ruby provides a method for scanning through string using regular expression called scan.


s = ‘barrack.obama@gmail.com’
s.scan /[\w+\.]{2,20}/


The above expression will result in an array as following,
[‘barrack.obama’, ‘gmail.com’]

See how easy it is. This expressions are not limited to ruby language, this is the same for all languages. This expression will work in most cases.

Let me explain the expression I have written above. [\w+\.]. what this says is that it will try to match to match words that includes underscore and dot. {2,20} means that the word that gets matched should have the length of 2 to 20.

This expression will help you to get the domain name of the user this email address belong to.

The scan method will always result in array, which might not be what we want all the time. There can be situations where we want to check if the string matches the certain pattern like if or while or case loops

For those cases, ruby provides a way to do that using a new operator =~.

Let’s assume that we want to check if the file has an extension .jpg.
image = ‘bacd.jpg’

you can always do like


image =~ /\w+\.(JPG)$/i


The expression mentioned above matches any string that starts with and ends with .jpg irrespective of the case.

There is one powerful ruby method for searching and replacing the set of words called ‘gsub’. Let’s assume we have a word ‘hello123’ and we want to replace 123 with ABC. This can be accomplished easily with gsub. The following syntax


litter = ‘hello123’
litter.gsub(/\d+/, ‘ABC’)


The above statements will result in ‘helloABC’. There are lots of things in ruby which I haven’t explored yet. Those may follow next in the post.

That’s all for this post I have...