eloquent ruby chapter 4 - find the right string with regular expression

11
 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

Upload: kuy-seng-chheoun

Post on 03-Jul-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

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

Page 2: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

●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

Page 3: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

●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.

Page 4: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

●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 => ?

Page 5: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

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]*, [0­9]*, [0­9a­f]*,   .* , .. , .*George, .*George.*

Page 6: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

Regular Expressions in Ruby In Ruby, the regular expression, or Regexp for 

short, is one of the built­in 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

Page 7: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

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)/, '**:** **' )

Page 8: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

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\.$/

Page 9: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

In the Wild

? The question mark matches zero or one of the things

Page 10: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

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) 456­7890 '123­456(789)[0]' => (123) 456­7890

Page 11: Eloquent Ruby chapter 4 - Find The Right String with Regular Expression

  

References

Addison Wesley Eloquent Ruby http://net.tutsplus.com/tutorials/ruby/ruby­for­newbies­regular­expressions/

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm