SlideShare a Scribd company logo
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
                       
●   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.
   dd:dd (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]*, [0­9]*, [0­9a­f]*,  
   .* , .. , .*George, .*George.*




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




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

                         
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





                                  

More Related Content

KEY
Andrei's Regex Clinic
PPT
Introduction to regular expressions
PPT
Php Chapter 4 Training
PPTX
Regular expression
KEY
Regular Expressions 101
PPT
Introduction to Regular Expressions
PPT
Textpad and Regular Expressions
PPTX
Introduction to Regular Expressions
Andrei's Regex Clinic
Introduction to regular expressions
Php Chapter 4 Training
Regular expression
Regular Expressions 101
Introduction to Regular Expressions
Textpad and Regular Expressions
Introduction to Regular Expressions

What's hot (20)

PPTX
Regular Expressions 101 Introduction to Regular Expressions
PPTX
Regular expressions
PDF
Regular expressions in Ruby and Introduction to Vim
PPTX
NLP_KASHK:Regular Expressions
ODP
Looking for Patterns
PPT
PERL Regular Expression
PPT
Bioinformatica 06-10-2011-p2 introduction
PPTX
Regex lecture
PPTX
Regular expressions
PPT
Regular Expressions 2007
PPT
Regular Expressions grep and egrep
PPTX
Regular expressions
PDF
Variable Naming Best Practices
PPT
Perl Intro 5 Regex Matches And Substitutions
PPTX
Regular Expression Crash Course
PDF
Ch02
ODP
Regular Expressions and You
PPT
Regular Expressions in PHP, MySQL by programmerblog.net
PPTX
Bioinformatics p2-p3-perl-regexes v2014
PPT
Regular Expressions 101 Introduction to Regular Expressions
Regular expressions
Regular expressions in Ruby and Introduction to Vim
NLP_KASHK:Regular Expressions
Looking for Patterns
PERL Regular Expression
Bioinformatica 06-10-2011-p2 introduction
Regex lecture
Regular expressions
Regular Expressions 2007
Regular Expressions grep and egrep
Regular expressions
Variable Naming Best Practices
Perl Intro 5 Regex Matches And Substitutions
Regular Expression Crash Course
Ch02
Regular Expressions and You
Regular Expressions in PHP, MySQL by programmerblog.net
Bioinformatics p2-p3-perl-regexes v2014
Ad

Viewers also liked (7)

PDF
Google guide - Make searching even easier
PDF
Will paginate and kaminari
PDF
Chapter17 of clean code
PPTX
Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...
PDF
Factory patterns
PDF
Edition T0P0L0G1ES
PDF
Mediator Design Pattern
Google guide - Make searching even easier
Will paginate and kaminari
Chapter17 of clean code
Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...
Factory patterns
Edition T0P0L0G1ES
Mediator Design Pattern
Ad

Similar to Eloquent Ruby chapter 4 - Find The Right String with Regular Expression (20)

PPT
Regular Expressions
PDF
Lecture 23
PDF
Working with text, Regular expressions
PDF
An Introduction to Regular expressions
PPTX
Regular Expressions in Stata
PPT
Class 5 - PHP Strings
PDF
3.2 javascript regex
PPT
Regular expressions
KEY
Regular expressions
PDF
Introduction_to_Regular_Expressions_in_R
PPTX
Regular_Expressions.pptx
ODP
Regular Expression
PPTX
Strings,patterns and regular expressions in perl
PPTX
Unit 1-strings,patterns and regular expressions
PPTX
Javascript正则表达式
PPT
Introduction to Regular Expressions RootsTech 2013
DOCX
Python - Regular Expressions
PDF
Regular expressions quick reference
PDF
RegEx Book.pdf
PPTX
Regular expressions
Regular Expressions
Lecture 23
Working with text, Regular expressions
An Introduction to Regular expressions
Regular Expressions in Stata
Class 5 - PHP Strings
3.2 javascript regex
Regular expressions
Regular expressions
Introduction_to_Regular_Expressions_in_R
Regular_Expressions.pptx
Regular Expression
Strings,patterns and regular expressions in perl
Unit 1-strings,patterns and regular expressions
Javascript正则表达式
Introduction to Regular Expressions RootsTech 2013
Python - Regular Expressions
Regular expressions quick reference
RegEx Book.pdf
Regular expressions

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.    
  • 4. Sets, Ranges, and Alternatives (cont') ●  Vertical bar character | use for alternatives.  dd:dd (AM|PM) #simplify => ?  A.M.|AM|P.M.|PM  #simplify => ?    
  • 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]*, [0­9]*, [0­9a­f]*,    .* , .. , .*George, .*George.*    
  • 6. 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    
  • 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) 456­7890  '123­456(789)[0]' => (123) 456­7890    
  • 11. 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     

Editor's Notes

  • #5: A\\.M\\.|AM|P\\.M\\. use new way to make it short. Reg = /(A|P)(\\.)?M/ reg = /(A|P)(\\.)?M|(a|p)(\\.)?m/
  • #6: Most regular expression is the same for all language with the same rules and syntax
  • #9: '123-456(789)[0]'.gsub(/\\D/,"").sub /(\\d{3})(\\d{3})(\\d{4})/,'(\\1) \\2-\\3'
  • #11: '123-456(789)[0]'.gsub(/\\D/,"").sub /(\\d{3})(\\d{3})(\\d{4})/,'(\\1) \\2-\\3'