This document discusses regular expressions in Ruby. It covers matching single characters, sets and ranges, using the asterisk for zero or more matches, Ruby's regex syntax using forward slashes, matching beginnings and endings, optional matches with question marks, and provides references for further reading.
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
1. CHAPTER 5
Find the Right String with
Regular Expressions
Matching One Character at a Time
Sets, Ranges, and Alternatives
The Regular Expression Star
Regular Expressions in Ruby
Beginnings and Endings
In the Wild
Staying Out of Trouble
2. ● Matching One Character at a Time
letters and numbers match themselves
Regular expressions is case sensitive. (default)
Unlike letters and numbers, most of the punctuation
characters—things like . and *—have special
meanings in regular expressions. ( e.g the period
or dot character matches any single character
except a newline character)
You use a backslash to turn off the special
meanings of the punctuation characters
3. ●Sets, Ranges, and Alternatives
To create a regular expression set, you wrap the
characters in square brackets
A Range by specifying the beginning and end of a
sequence of characters, separated by a dash
Shortcut for range:
d will match any digit
w, where the w stands for “word character,” will
match any letter, number or the underscore.
s will match any white space character including the
vanilla space, the tab, and the newline.
5. The Regular Expression Star
In regular expressions, an asterisk (*) matches zero
or more of the thing that came just before it
A* , AB*, R*uby, R*u*by
[aeiou]*, [09]*, [09af]*,
.* , .. , .*George, .*George.*
6. Regular Expressions in Ruby
In Ruby, the regular expression, or Regexp for
short, is one of the builtin data types, with its
own special literal syntax. Encase pattern
between forward slashes. (like JavaScript)
You use the =~ operator to test whether a regular
expression matches a string. return char index
when match and nil when not.
class Regexp
def test(str)
(self =~ str) ? false : true
end
end
7. Regular Expressions in Ruby (cont')
Turn case sensitivity off my sticking an i on the end
of your expression
Regular expressions also come into play in the
string methods that involve searching e.g. 'sub',
'gsub'
@content.gsub!( /dd:dd (AM|PM)/, '**:** **' )
8. Beginnings and Endings
A (capital letter A) match the beginning or
circumflex ^
z (note the lower case) or sign $ matches the end of
the string
The .* won’t match across the lines . . . unless we
simply turn off this behavior by adding an m to
our expression
e.g /^Once upon a time.*happily ever after.$/
9. In the Wild
? The question mark matches zero or one of the
things
10. Bonus
'^' in Set vs '^' for beginning. e.g. /[^aeiou]/,
/^[aeiou]
d & D, w & W
+, {n}, {n,},{n,n}, # n is number
$n # n is number
n # n is number in string with single quote
String.match reg # == reg.match String
'1234567890' => (123) 4567890
'123456(789)[0]' => (123) 4567890