Regular Expressions in Ruby

Our example demonstrates common regular expression tasks in Ruby.

require 'regex'

# This tests whether a pattern matches a string.
match = /p([a-z]+)ch/.match?("peach")
puts match

# For other regex tasks, you'll use the Regexp class.
r = Regexp.new('p([a-z]+)ch')

# Many methods are available on these objects. Here's
# a match test like we saw earlier.
puts r.match?("peach")

# This finds the match for the regexp.
puts r.match("peach punch")[0]

# This also finds the first match but returns the
# start and end indexes for the match instead of the
# matching text.
puts "idx: #{r.match("peach punch").offset(0)}"

# The MatchData object includes information about
# both the whole-pattern matches and the submatches
# within those matches.
m = r.match("peach punch")
puts [m[0], m[1]]

# This will return information about the
# indexes of matches and submatches.
puts r.match("peach punch").offset(0) + r.match("peach punch").offset(1)

# To find all matches for a regexp, use scan.
puts "peach punch pinch".scan(r)

# Providing a block to scan will yield each match.
result = []
"peach punch pinch".scan(r) do |match|
  result << match.offset(0) + match.offset(1)
end
puts "all: #{result}"

# Limiting the number of matches:
puts "peach punch pinch".scan(r)[0..1]

# You can also use regex with byte strings.
puts r.match?("peach".b)

# When creating global variables with regular
# expressions, you can use the %r syntax.
R = %r{p([a-z]+)ch}
puts "regexp: #{R}"

# The gsub method can be used to replace
# subsets of strings with other values.
puts "a peach".gsub(r, '<fruit>')

# The gsub method also accepts a block for
# custom transformations.
puts "a peach".gsub(r) { |match| match.upcase }

To run the program:

$ ruby regular_expressions.rb
true
true
peach
idx: [0, 5]
["peach", "ea"]
[0, 5, 1, 3]
["peach", "punch", "pinch"]
all: [[0, 5, 1, 3], [6, 11, 7, 9], [12, 17, 13, 15]]
["peach", "punch"]
true
regexp: (?-mix:p([a-z]+)ch)
a <fruit>
a PEACH

For a complete reference on Ruby regular expressions, check the Regexp class documentation.