SlideShare a Scribd company logo
1 of 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
                       
●   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

What's hot

Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsDanny Bryant
 
Regular expressions in Ruby and Introduction to Vim
Regular expressions in Ruby and Introduction to VimRegular expressions in Ruby and Introduction to Vim
Regular expressions in Ruby and Introduction to VimStalin Thangaraj
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for PatternsKeith Wright
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular ExpressionBinsent Ribera
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007Geoffrey Dunn
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrepTri Truong
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsEran Zimbler
 
Variable Naming Best Practices
Variable Naming Best PracticesVariable Naming Best Practices
Variable Naming Best PracticesShahid Rasheed
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsShaun Griffith
 
Regular Expression Crash Course
Regular Expression Crash CourseRegular Expression Crash Course
Regular Expression Crash CourseImran Qasim
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and YouJames Armes
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netProgrammer Blog
 

What's hot (20)

Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions in Ruby and Introduction to Vim
Regular expressions in Ruby and Introduction to VimRegular expressions in Ruby and Introduction to Vim
Regular expressions in Ruby and Introduction to Vim
 
NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions
 
Looking for Patterns
Looking for PatternsLooking for Patterns
Looking for Patterns
 
PERL Regular Expression
PERL Regular ExpressionPERL Regular Expression
PERL Regular Expression
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expressions 2007
Regular Expressions 2007Regular Expressions 2007
Regular Expressions 2007
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Variable Naming Best Practices
Variable Naming Best PracticesVariable Naming Best Practices
Variable Naming Best Practices
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
 
Regular Expression Crash Course
Regular Expression Crash CourseRegular Expression Crash Course
Regular Expression Crash Course
 
Ch02
Ch02Ch02
Ch02
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
Regular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.netRegular Expressions in PHP, MySQL by programmerblog.net
Regular Expressions in PHP, MySQL by programmerblog.net
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 
Les08
Les08Les08
Les08
 

Viewers also liked

Google guide - Make searching even easier
Google guide - Make searching even easierGoogle guide - Make searching even easier
Google guide - Make searching even easierKuyseng Chhoeun
 
Will paginate and kaminari
Will paginate and kaminariWill paginate and kaminari
Will paginate and kaminariKuyseng Chhoeun
 
Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...
Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...
Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...ProgrammableWeb
 

Viewers also liked (7)

Google guide - Make searching even easier
Google guide - Make searching even easierGoogle guide - Make searching even easier
Google guide - Make searching even easier
 
Will paginate and kaminari
Will paginate and kaminariWill paginate and kaminari
Will paginate and kaminari
 
Chapter17 of clean code
Chapter17 of clean codeChapter17 of clean code
Chapter17 of clean code
 
Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...
Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...
Innovation showcase: Markus Lanthaler, Developer, Consultant, Researcher,mark...
 
Factory patterns
Factory patternsFactory patterns
Factory patterns
 
Edition T0P0L0G1ES
Edition T0P0L0G1ESEdition T0P0L0G1ES
Edition T0P0L0G1ES
 
Mediator Design Pattern
Mediator Design PatternMediator Design Pattern
Mediator Design Pattern
 

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

Lecture 23
Lecture 23Lecture 23
Lecture 23rhshriva
 
An Introduction to Regular expressions
An Introduction to Regular expressionsAn Introduction to Regular expressions
An Introduction to Regular expressionsYamagata Europe
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP StringsAhmed Swilam
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsJames Gray
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RHellen Gakuruh
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionLambert Lum
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式ji guang
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Ben Brumfield
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
Regular expressions quick reference
Regular expressions quick referenceRegular expressions quick reference
Regular expressions quick referencejvinhit
 

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

Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
An Introduction to Regular expressions
An Introduction to Regular expressionsAn Introduction to Regular expressions
An Introduction to Regular expressions
 
Regular Expressions in Stata
Regular Expressions in StataRegular Expressions in Stata
Regular Expressions in Stata
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Introduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_RIntroduction_to_Regular_Expressions_in_R
Introduction_to_Regular_Expressions_in_R
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
Regular expressions quick reference
Regular expressions quick referenceRegular expressions quick reference
Regular expressions quick reference
 
RegEx Book.pdf
RegEx Book.pdfRegEx Book.pdf
RegEx Book.pdf
 
Regular expressions
Regular expressionsRegular expressions
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

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