Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Ruby—A Diamond of a Programming Language, Part 2 : Page 3

Get ready to dive deeper into the power and elegance of the language lauded as a possible contender to replace current programming languages.


advertisement
Regular Expressions
Most programming languages utilize regular expressions. Ruby, having descended from many scripting languages, makes extensive use of regular expressions. A co-worker of mine once said "regular expressions aren't." Regular that is. In other words, regular expressions can take some effort and time to fully understand. You will only get a glimpse of the power of Ruby's regular expressions here. You don’t have to use regular expressions to write your applications, but suffice it to say it can make writing applications that work with Strings, in particular, much tighter and easier to write. This section will get you started, but you have some homework to research expressions if you are going to be a Ruby guru.

A regular expression "is a string that describes or matches a set of strings" based on some defined syntax. Another term for regular expression is "pattern." Regular expressions are used to do string manipulation or searching. You have probably used a regular expression when looking for files on your hard drive. For example, if you want to delete all the files that begin with "ruby" in a particular directory, you might use the rm ruby* command on your Windows or Unix box. In this simple example, ruby* is the regular expression you use to describe or match files.

Regular expressions in Ruby are defined between Tiger or Phil.



/Tiger|Phil/

You can now use this regular expression with a match operator (“=~”) in a conditional or loop statement to match or find other strings.

irb(main):001:0> golfer="Davis" if golfer =~ /Tiger|Phil/ puts "This is going to be a long drive." else puts "And now a drive by " + golfer end => "Davis" And now a drive by Davis

Here is another regular expression that is a bit more complex: /[\w._%-]+@[\w.-]+.[a-zA-Z]{2,4}/. Can you guess what this expression represents? If you guessed an email address, you are right. This regular expression could be used to validate email addresses.

Editor's Note: A sharp-eyed reader, Sam Livingston-Gray, sent us this alternative regular expression, which is a bit more rigid about validating email addresses. Our thanks to Sam for the suggestion.

/\A[\w\._%-]+@[\w\.-]+\.[a-zA-Z]{2,4}\z/

irb(main):001:0> emailRE= /[\w._%-]+@[\w.-]+.[a-zA-Z]{2,4}/ email = "jwhite@interechtraining.com" if email =~ emailRE puts "This is a valid email address." else puts "this is not a valid email address." end This is a valid email address. irb(main):002:0> email = "###@spammer&&&.333" if email =~ emailRE puts "This is a valid email address." else puts "this is not a valid email address." end this is not a valid email address.

Figure 2 dissects the email regular expression. As you can see, the regular expression language is quite rich and unfortunately cannot be covered in detail here. For more information, take a look at http://www.regular-expressions.info.

Figure 2. Email Regular Expression: The regular expression to describe email addresses may look complex but it is really just a set of patterns that describe each section of the email address – user, domain and qualifier.

And if you haven't guessed it by now, regular expressions live up to the Ruby mantra "everything is an object" because regular expressions are objects too. In the code example below, a regular expression instance (from class Regexp) is used as a parameter to a String method, gsub, to replace "happy" and "joy" with "glad."

irb(main):001:0> quote = "I am so happy. Happy, happy, joy, joy!" regx = /(h|H)appy|joy/ quote.gsub(regx, "glad") => "I am so happy. Happy, happy, joy, joy!" => /(h|H)appy|joy/ => "I am so glad. glad, glad, glad, glad!"

When you use the =~ operator on the regular expression object, you can get information such as the index of the matching pattern string.

irb(main):001:0> /Tiger|Phil/=~"EyeOfTheTiger" => 8

The power of regular expression in Ruby becomes pretty apparent if you have ever had to write an application that works heavily with strings. While a bit chewy, you owe it to yourself to fully explore regular expressions in Ruby before you get too deep in coding your first application. Take a look at the sample application attached to this article for more examples of regular expressions in Ruby.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap