Regular Expressions in Ruby

Regular Expressions are a powerful tool to process text and extract information from it. The idea behind regular expressions is that you can define an expression to check if an input string matches a certain pattern. And you can manipulate or extract pieces of information from the string according to your needs. It’s very useful because instead of coding a long program to manipulate a string, you just have to create an expression.

In Ruby, the =~ operator is used for matching regular expressions. Example:

 >> sentence = "This is just a test."
=> "This is just a test."
>> sentence =~ /^[A-Z].*[?!.]$/
=> 0

In the example above, the regular expression matches the string provided (remember that in Ruby zero evaluates to true). The regular expression in the example is /^[A-Z].*[?!.]$/ . When using the =~ operator, the regular expression must be provided between slashes. This expression checks the following pattern:

  • ^: this character anchors the pattern to the beginning of the string.
  • [A-Z]: the first character in the sentence has to be in the range A-Z (uppercase letters only).
  • .*: the dot matches any character, and the asterisk means that this pattern can repeat zero or many times. Therefore, it will match any sequence of characters, or no characters at all.
  • [?!.]: matches one ocurrence of any of these punctuation marks.
  • $: anchors the pattern to the end of the string.

So, in resume, this regular expression will match any string that starts with an uppercase letter (A-Z) and finishes with a punctuation mark (dot, interrogation or exclamation). Note that the square brackets are used to define ranges or lists of characters in the expression.

You can find more information about Ruby’s regular expressions at http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm.

Advertisement

Ruby

After a long time dealing with boring programming languages, I’m really excited for having been introduced to Ruby. In my opinion, programming languages have to be compact, or, to use a better word: minimalist. And this is a great characteristic of Ruby. You don’t have to spend ages to learn it, specially if you already know how to program in another Object-Oriented language.

I’m gonna list some of the great characteristics of Ruby:

  1. It’s a minimalist language, as I already noted. The syntax is easy to learn.
  2. Strong Object-Orientation. Everything is an object!
  3. Since everything is an object, every operation is a method call. Even basic arithmetic operations (+, -, etc.) are method calls.
  4. Metaprogramming: you can alter methods and classes at any time, even when the program is running. You can even change the methods from the basic classes of the language such as String or Integer.

Other positive points (in my subjective opinion) are that it’s extremelly compatible Linux, BSD or any other Unix-based operating system; you can use an interactive interpreter to develop, which makes it really easy to test and run pieces of code; and there is an excellent framework for agile web development available for Ruby, called Rails. It’s more usual to see the term Ruby on Rails; just remember that Ruby is the language, and Rails is the framework.