1. Computing

Regular Expression Options

By

In addition to the regular expression language features in Ruby, there are also four options that may be appended to regular expression literals that will change how the regular expression acts. While some of these are not frequently used, they can help you in some tricky situations.

The Syntax

The four options i, o, m and x are turned on by placing them after the terminating slash character of a regular expression literal. Note that putting these options there will turn them on for the entire regular expression.

For example, if you want to turn on the i and o options in the regex /ab[123]+c/, the regex would look like this:

/ab[123]+c/io.

What do the Options Do?

There are four options you can turn on in your regexen. Each one does something different as a way to change how the regex will act.

  • Case Insensitive: i - Using this option will make the regex case insensitive, meaning upper- and lower-case letters will be treated the same. If the regex is /abc/i, it will also match the strings "aBc", "ABc" and "ABC", or any other combination of upper- and lower-case letters.

  • Multi-line: m - Normally, regular expressions are only capable of matching single-line strings. If the m option is turned on, the . element will match any character including the newline character. This allows you to easily create regex that will match strings which span multiple lines. The regex /abc.+def/m will match the string "abc \ndef", but it would not normally do so if the m option wasn't enabled.

  • Single interpolation: o - This allows for the regex to only interpolate #{...} blocks once. Normally, the blocks will be interpolated every time the regex object is evaluated. With the o option, the regex will only interpolate its blocks once--the first time it is evaluated itself--and will store the results. The following example illustrates the difference between a regex with and without the o option.

     #!/usr/bin/env ruby
     
     # Will only match the first time
     # The num variable will be 10 at
     # the end.
     num = 0
     10.times do
       puts /#{num += 1}/ =~ "1"
     end
     puts num
     
     # Will match every time
     # The num variable will be 1 at
     # the end
     num = 0
     10.times do
       puts /#{num += 1}/o =~ "1"
     end
     puts num
     
  • Expanded Mode: x - The expanded mode option causes unescaped whitespace in the regular expression itself to be ignored, as well as comments inside the regex. This option can make unreadable expressions readable by allowing you to put regular expression on multiple lines, and to even have comments when combined with the %r{} special regex.

     #!/usr/bin/env ruby
     
     puts %r{
      a+ # Some a characters
      [0-9]+ # Some numbers
      :[0-9] # A colon then a number
     }x =~ "aaa23:7"
     
     # This is functionally equivalent
     puts /a+[0-9]+:[0-9]/ =~ "aaa23:7"
     
  1. About.com
  2. Computing
  3. Ruby
  4. Regular Expressions
  5. Regular Expression Options

©2014 About.com. All rights reserved.