Training „git“
07.10.2010 - 08.10.2010, Essen
Training „Eclipse RCP“
28.03.2011 - 01.04.2011, Dortmund
19.07.2009

Ruby Regular Expression API cheat sheet

Creating a regular expression

>> /world/mix
=> Regexp(/world/mix)

>> Regexp.new("world", Regexp::MULTILINE | Regexp::IGNORECASE | Regexp::EXTENDED )
=> Regexp(/world/mix)

Finding matches

String.=~(Regexp) returns the starting position of the first match or nil if no match was found:

>> "123 456 789" =~ /\d+/
=> 0

>> "abc def ghi" =~ /\d+/
=> nil

>> if "123 456 789" =~ /\d+/ then "found" end
=> "found"

Special $ variables will contain information about the last match:

>> "123 456 789" =~ /(\d)(\d)(\d)/
=> 0

# $` contains text before last match
# $& contains last matched string
# $' contains text after last match
>> "#{$`}[#{$&}]#{$'}" 
=> "[123] 456 789"

# $n contains the n-th (...) capture of the last match
>> "#{$1}-#{$2}-#{$3}"
=> "1-2-3"

# $~ contains MatchData object
>> $~.captures
=> ["1", "2", "3"]

String.scan returns all matches as String array:

# regex without capture group
>> "123 456 789".scan(/\d+/)
=> ["123", "456", "789"]

# assigning matches to variables
>> first, second, third = "123 456 789".scan(/\d+/)

# regex with captures
>> "123 456 789".scan(/(\d)(\d)(\d)/)
=> [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]]

String.scan can also be used with a block:

# regex without captures
>> "123 456 789".scan(/\d+/) {|m| p m }
"123"
"456"
"789"

# regex with captures
>> "123 456 789".scan(/(\d)(\d)(\d)/) { |m| puts "#{m.inspect}, 1st capture: #{$1}" }
["1", "2", "3"], 1st capture: 1
["4", "5", "6"], 1st capture: 4
["7", "8", "9"], 1st capture: 7

Replacing matches

String.gsub returns a new String with matches replaced, String.gsub! changes the String directly:

# replacing with a string (use \1, \2 ... to refer to captures)
>> "123 456 789".gsub(/(\d+)/, '[\1]')
>> "123 456 789".gsub(/(\d+)/, "[\\1]")
=> "[123] [456] [789]"

# replacing with a block (using $ variables)
>> "123 456 789".gsub(/(\d+)/) { |m| m.to_i * 2 }
>> "123 456 789".gsub(/(\d+)/) { $1.to_i * 2 }
=> "246 912 1578"
I'm looking forward to your comments:

Schulungen

Ruby

Ralf Ebert | Blog | Ruby | Ruby Regular Expression API cheat sheet