eloquent ruby chapter 4 - find the right string with regular expression
TRANSCRIPT
CHAPTER 5Find 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
●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
●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.
●Sets, Ranges, and Alternatives (cont')
Vertical bar character | use for alternatives. \d\d:\d\d (AM|PM) #simplify => ? A\.M\.|AM|P\.M\.|PM #simplify => ?
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.*
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
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!( /\d\d:\d\d (AM|PM)/, '**:** **' )
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\.$/
In the Wild
? The question mark matches zero or one of the things
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
References
Addison Wesley Eloquent Ruby http://net.tutsplus.com/tutorials/ruby/rubyfornewbiesregularexpressions/
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm