Regular Expressions

Finding the first match

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

>> "found" if "123 456 789" =~ /\d+/
=> "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"

Accessing captures

$n contains the n-th (...) capture of the last match, $~ contains MatchData object:

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

>> [$1, $2]
=> ["12", "3"]

>> $~.captures
=> ["12", "3"]

Finding all matches

String.scan returns matching strings as array of Strings:

# get all matches for a 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:

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

# ruby 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"

Modifiers

/.*/m         multiline: . matches newline
/.*/i         ignore case
/.*/x         extended: ignore whitespace in pattern

Regular expressions by example

/a/           character 'a' 
/\//          character '/' (/\?*+{[.|()^$ need to be escaped with \)
/./           any character (including newline for /.../m)

/a?/          0..1 'a'
/a*/          0..n 'a'  
/a+/          1..n 'a'   
/a{2,7}/      2..7 'a'    
/a{2,}/       2..n 'a'    
/a{,7}/       0..7 'a'    

/a?bc?/       'b' or 'ab' or 'bc' or 'abc'
/a|bc/        'a' or 'bc'
/(a|b)c/      'ac' or 'bc'

/[abc]/       a or b or c
/[^abc]/      any character except a or b or c
/[a-cF-H]/    a or b or c or F or G or H

/\d/          any digit [0-9]
/\w/          any letters, numbers or underscores [a-zA-Z0-9_]
/\s/          any whitespace character (including newline for /.../m)

/\D/          any character except digits
/\W/          any character except letters, numbers or underscores
/\S/          any character except whitespace

/^abc/        abc after line start
/abc$/        abc before line end

More Information

Share
Twitter Facebook

Apps

website screenshot mac os x
Ralf Ebert Ralf Ebert is an independent software developer and trainer for Mac OS X and iOS. He makes the Page Layers and Straight ahead apps and conducts iOS trainings in Germany since 2009.