SlideShare a Scribd company logo
1 of 51
Download to read offline
Introduction
               Diving In
            Building up




/regular expressions/demystified
 From deckhand to pirate in 30 minutes


Philip Tellis / philip@bluesmoon.info
                      Yahoo!




                           /regular expressions/demystified
Introduction
                              Diving In
                           Building up


Outline

  1   Introduction
         Who’s playing?
         Conventions

  2   Diving In
        Starting Small
        Getting meta

  3   Building up
        More or less
        Alternation
        Groups


                                          /regular expressions/demystified
Introduction
                                    Who’s playing?
                        Diving In
                                    Conventions
                     Building up


$ whoami?




    Philip Tellis
    philip@bluesmoon.info
    @bluesmoon
    yahoo
    geek




                                    /regular expressions/demystified
Introduction
                                        Who’s playing?
                            Diving In
                                        Conventions
                         Building up


Who are you?




     Developer
     Curious
     Interested in regular expressions
     You may or may not have used them before




                                        /regular expressions/demystified
Introduction
                                        Who’s playing?
                            Diving In
                                        Conventions
                         Building up


What is a regular expression?




     A pattern that can match multiple strings
     A pattern matching language
     A Finite Automaton




                                        /regular expressions/demystified
Introduction
                                         Who’s playing?
                             Diving In
                                         Conventions
                          Building up


What is a regular expression?




      But this is a hacker session, so let’s forget the theory.
                   (You can read the book later.)




                                         /regular expressions/demystified
Introduction
                                         Who’s playing?
                             Diving In
                                         Conventions
                          Building up


What is a regular expression?




      But this is a hacker session, so let’s forget the theory.
                   (You can read the book later.)




                                         /regular expressions/demystified
Introduction
                                         Who’s playing?
                             Diving In
                                         Conventions
                          Building up


Conventions used in this talk



      Text in ’single quotes’ denotes a literal string
      Text in /forward slashes/ denotes a regular
      expression
      The operator =∼ indicates that the string on the left
      matches the pattern on the right
      The operator !∼ indicates that the string on the left does
      not match the pattern on the right
      $string denotes a variable containing a string




                                         /regular expressions/demystified
Introduction
                                     Starting Small
                         Diving In
                                     Getting meta
                      Building up


Match a single character




        ’a’ =~ /a/




                                     /regular expressions/demystified
Introduction
                                      Starting Small
                          Diving In
                                      Getting meta
                       Building up


Let’s try a different character




         ’t’ =~ /t/




                                      /regular expressions/demystified
Introduction
                                         Starting Small
                             Diving In
                                         Getting meta
                          Building up


Building up




  Combine the previous two into a single regular expression

        ’at’ =~ /at/




                                         /regular expressions/demystified
Introduction
                                         Starting Small
                             Diving In
                                         Getting meta
                          Building up


You now know regular expressions




    To build a regular expression, break the pattern into small
      manageable pieces and incrementally combine them.




                                         /regular expressions/demystified
Introduction
                                       Starting Small
                           Diving In
                                       Getting meta
                        Building up


Metacharacters



     The regex language has its own syntax characters to do
     funky things
     Some of these act as wild cards
     Others act as modifiers to whatever comes before them
     And some of them make your brain explode
     We won’t be blowing up brains today




                                       /regular expressions/demystified
Introduction
                                       Starting Small
                           Diving In
                                       Getting meta
                        Building up


Metacharacters



     The regex language has its own syntax characters to do
     funky things
     Some of these act as wild cards
     Others act as modifiers to whatever comes before them
     And some of them make your brain explode
     We won’t be blowing up brains today




                                       /regular expressions/demystified
Introduction
                                       Starting Small
                           Diving In
                                       Getting meta
                        Building up


Metacharacters



     The regex language has its own syntax characters to do
     funky things
     Some of these act as wild cards
     Others act as modifiers to whatever comes before them
     And some of them make your brain explode
     We won’t be blowing up brains today




                                       /regular expressions/demystified
Introduction
                                       Starting Small
                           Diving In
                                       Getting meta
                        Building up


The . metacharacter


     Matches ONE and ONLY ONE character
              ’a’     =~    /./
              ’b’     =~    /./
              ’c’     =~    /./
              ’’      !~    /./

     The empty string has less than ONE character
     ’abc’ has ONE character. . . three times
              ’abc’ =~ /./




                                       /regular expressions/demystified
Introduction
                                       Starting Small
                           Diving In
                                       Getting meta
                        Building up


The . metacharacter


     Matches ONE and ONLY ONE character
              ’a’     =~    /./
              ’b’     =~    /./
              ’c’     =~    /./
              ’’      !~    /./

     The empty string has less than ONE character
     ’abc’ has ONE character. . . three times
              ’abc’ =~ /./




                                       /regular expressions/demystified
Introduction
                                       Starting Small
                           Diving In
                                       Getting meta
                        Building up


The . metacharacter


     Matches ONE and ONLY ONE character
              ’a’     =~    /./
              ’b’     =~    /./
              ’c’     =~    /./
              ’’      !~    /./

     The empty string has less than ONE character
     ’abc’ has ONE character. . . three times
              ’abc’ =~ /./




                                       /regular expressions/demystified
Introduction
                                            Starting Small
                                Diving In
                                            Getting meta
                             Building up


The fate of gate hate date



             /.ate/




 Matches                                    Does not match
     aate bate cate date . . .                 ate
     crates abates dates elated                     ates ated
     ...
     @ate 9ate ’ ate’



                                            /regular expressions/demystified
Introduction
                                            Starting Small
                                Diving In
                                            Getting meta
                             Building up


The fate of gate hate date



             /.ate/




 Matches                                    Does not match
     aate bate cate date . . .                 ate
     crates abates dates elated                     ates ated
     ...
     @ate 9ate ’ ate’



                                            /regular expressions/demystified
Introduction
                                           Starting Small
                               Diving In
                                           Getting meta
                            Building up


Character classes



            /[a-z]ate/




Matches                                    Does not match
    aate bate cate date . . .                 ate
    crates abates dates elated                     ates ated
    ...                                            @ate 9ate ’ ate’




                                           /regular expressions/demystified
Introduction
                                          Starting Small
                              Diving In
                                          Getting meta
                           Building up


Character classes



  To match a literal ’-’ it should be the first or last character in
  the class:

         /[+-*/]/                     # Incorrect


         /[+*/-]/                     # Correct




                                          /regular expressions/demystified
Introduction
                                       Starting Small
                           Diving In
                                       Getting meta
                        Building up


Negated character classes



           /[^a-z]ate/




Matches                                Does not match
    @ate 9ate ’ ate’                      ate ates ated
    g@ate e9ated                               aate bate cate date . . .
                                               crates abates dates elated
                                               ...



                                       /regular expressions/demystified
Introduction
                                          Starting Small
                              Diving In
                                          Getting meta
                           Building up


The late fate of gate hate date rate



            /[df-hlr]ate/




 Matches                                  Does not match
     date fate gate hate late                ate aate bate cate eate
     rate                                    iate jate kate . . .
     dates fated billgates hated
     ...



                                          /regular expressions/demystified
Introduction
                                          Starting Small
                              Diving In
                                          Getting meta
                           Building up


The late fate of gate hate date rate



            /[df-hlr]ate/




 Matches                                  Does not match
     date fate gate hate late                ate aate bate cate eate
     rate                                    iate jate kate . . .
     dates fated billgates hated
     ...



                                          /regular expressions/demystified
Introduction
                                         Starting Small
                             Diving In
                                         Getting meta
                          Building up


Anchors



           /^[df-hlr]ate$/




Matches                                  Does not match
    date fate gate hate late                ate aate bate . . .
    rate                                         dates gated berate elated
                                                 ...




                                         /regular expressions/demystified
Introduction
                                        Starting Small
                            Diving In
                                        Getting meta
                         Building up


Anchors




     ˆ matches the start of the string
     $ matches the end of the string
     Both are 0 byte matches, ie, they do not match any
     character




                                        /regular expressions/demystified
Introduction    More or less
                           Diving In   Alternation
                        Building up    Groups


Matching more than one of something




     ? – matches 0 or 1 of what comes before it
     * – matches 0 or more of what comes before it
     + – matches 1 or more of what comes before it
     {n,m} – matches between n and m of what comes before it




                                       /regular expressions/demystified
Introduction    More or less
              Diving In   Alternation
           Building up    Groups


Aaargh!




                          Everyone shout “Aaarrrgh!”




                          /regular expressions/demystified
Introduction    More or less
                          Diving In   Alternation
                       Building up    Groups


How many ways can you say Aargh!?



     argh
     aaaaaargh
     aaaarrrrghhh
     aaaaarrrrrggggghhhh
     aaarrrrggggg
     aaaaarrrrrhhhh




                                      /regular expressions/demystified
Introduction    More or less
                            Diving In   Alternation
                         Building up    Groups


Match ’em all




       /a+r+g+h+/           #     aarrrrgggghhhh
       /a+r+g+h*/           #     aarrgghh & aarrgg
       /a+r+g*h+/           #     aarrgghh & aarrhh
       /a+r+g*h*/           #     argh & arg & arh


  That last one also matches ’ar’ which we don’t want




                                        /regular expressions/demystified
Introduction    More or less
                            Diving In   Alternation
                         Building up    Groups


Match ’em all




       /a+r+g+h+/           #     aarrrrgggghhhh
       /a+r+g+h*/           #     aarrgghh & aarrgg
       /a+r+g*h+/           #     aarrgghh & aarrhh
       /a+r+g*h*/           #     argh & arg & arh


  That last one also matches ’ar’ which we don’t want




                                        /regular expressions/demystified
Introduction    More or less
                            Diving In   Alternation
                         Building up    Groups


Match ’em all




       /a+r+g+h+/           #     aarrrrgggghhhh
       /a+r+g+h*/           #     aarrgghh & aarrgg
       /a+r+g*h+/           #     aarrgghh & aarrhh
       /a+r+g*h*/           #     argh & arg & arh


  That last one also matches ’ar’ which we don’t want




                                        /regular expressions/demystified
Introduction    More or less
                            Diving In   Alternation
                         Building up    Groups


Match ’em all




       /a+r+g+h+/           #     aarrrrgggghhhh
       /a+r+g+h*/           #     aarrgghh & aarrgg
       /a+r+g*h+/           #     aarrgghh & aarrhh
       /a+r+g*h*/           #     argh & arg & arh


  That last one also matches ’ar’ which we don’t want




                                        /regular expressions/demystified
Introduction    More or less
                            Diving In   Alternation
                         Building up    Groups


Match ’em all




       /a+r+g+h+/           #     aarrrrgggghhhh
       /a+r+g+h*/           #     aarrgghh & aarrgg
       /a+r+g*h+/           #     aarrgghh & aarrhh
       /a+r+g*h*/           #     argh & arg & arh


  That last one also matches ’ar’ which we don’t want




                                        /regular expressions/demystified
Introduction    More or less
                           Diving In   Alternation
                        Building up    Groups


Alternation: Match all this or all that




        /ab|cd/


  Matches either ’ab’ or ’cd’




                                       /regular expressions/demystified
Introduction    More or less
                              Diving In   Alternation
                           Building up    Groups


From here to eternity

  | matches either everything on its left or everything on its right
            (That’s a pipe character, not the letter I)




                                          /regular expressions/demystified
Introduction    More or less
                            Diving In   Alternation
                         Building up    Groups


Back to aaargh



       /g*h+|g+h*/


  This matches all the endings we want:
      ggggghhhhhh
      ggggg
      hhhhh




                                        /regular expressions/demystified
Introduction    More or less
                                Diving In   Alternation
                             Building up    Groups


Back to aaargh


       /a+r+g*h+|g+h*/


  This doesn’t quite work

Matches                                     Does not match
    aaarrrhhh                                  aaarrrggg
    aaarrrrggghhh
    gggg
    gggghhhh


                                            /regular expressions/demystified
Introduction    More or less
                                Diving In   Alternation
                             Building up    Groups


Back to aaargh


       /a+r+g*h+|g+h*/


  This doesn’t quite work

Matches                                     Does not match
    aaarrrhhh                                  aaarrrggg
    aaarrrrggghhh
    gggg
    gggghhhh


                                            /regular expressions/demystified
Introduction    More or less
                          Diving In   Alternation
                       Building up    Groups


Group the subexpression



      /a+r+(g*h+|g+h*)/



  Matches
      aaarrrhhh
      aaarrrggg
      aaarrrrggghhh




                                      /regular expressions/demystified
Introduction    More or less
                            Diving In   Alternation
                         Building up    Groups


Grouping parentheses




     ( and ) mark a group
     | alternates within a group
     Groups may be nested - it’s like a new regex inside
     +, *, ? and {n,m} may apply to an entire group




                                        /regular expressions/demystified
Introduction
           Diving In
        Building up


Stop




                       /regular expressions/demystified
Introduction
                          Diving In
                       Building up


Summary




    Start small, match the parts you understand
    Build up to more complex patterns
    Not all problems should be solved by regular expressions




                                      /regular expressions/demystified
Introduction
                            Diving In
                         Building up


More Info. . .




      “Mastering Regular Expressions” – Jeffrey Friedl
      http://tech.bluesmoon.info/search/label/regex




                                        /regular expressions/demystified
Introduction
                       Diving In
                    Building up


Contact me




  Philip Tellis
  philip@bluesmoon.info
  @bluesmoon
  bluesmoon.info




                                   /regular expressions/demystified
Introduction
                             Diving In
                          Building up


Image credits




     http://flickr.com/photos/practicalowl/3933514241/
     http://flickr.com/photos/loozrboy/3908830690/
     http://flickr.com/photos/thetruthabout/2680546103/
     http://flickr.com/photos/donsolo/2136923757/




                                         /regular expressions/demystified
Introduction
    Diving In
 Building up




     Thank You




                /regular expressions/demystified
Introduction
                          Diving In
                       Building up


Aargh with class



      /a+r+g*[gh]h*/



  Matches
      aaarrrhhh
      aaarrrggg
      aaarrrrggghhh




                                      /regular expressions/demystified
Introduction
                       Diving In
                    Building up


Matching meta characters in a character class




        /[a-zA-Z0-9_-]/
        /[a-z^]/
        /[][]/




                                   /regular expressions/demystified
Introduction
                        Diving In
                     Building up


Alternating multiple items




       /apples|oranges|bananas/

       /buy some (apples|oranges|ba(na){2}s)/




                                    /regular expressions/demystified

More Related Content

Viewers also liked

EtCETra 2k15- Entertainment Quiz Finals
EtCETra 2k15- Entertainment Quiz FinalsEtCETra 2k15- Entertainment Quiz Finals
EtCETra 2k15- Entertainment Quiz FinalsBidit Mohanty
 
English teachers who blog
English teachers who blogEnglish teachers who blog
English teachers who blogDET
 
Software Radio Implementation: A Systems Perspective
Software Radio Implementation: A Systems PerspectiveSoftware Radio Implementation: A Systems Perspective
Software Radio Implementation: A Systems PerspectiveSteve Muir
 
Expectation of Corporates from Professionals
Expectation of Corporates from ProfessionalsExpectation of Corporates from Professionals
Expectation of Corporates from Professionalsdeepak1983
 
Learning from games : Dr Joanne O'Mara
Learning from games : Dr Joanne O'MaraLearning from games : Dr Joanne O'Mara
Learning from games : Dr Joanne O'MaraPublicLibraryServices
 
Social Media in the Classroom - ICTEV Conf Gail Casey May12
Social Media in the Classroom - ICTEV Conf Gail Casey May12Social Media in the Classroom - ICTEV Conf Gail Casey May12
Social Media in the Classroom - ICTEV Conf Gail Casey May12Gail Casey
 
Hamare Rusoom wa Quyood: Syed Ali Naqi Naqvi Sahab t.s.
Hamare Rusoom wa Quyood: Syed Ali Naqi Naqvi Sahab t.s.Hamare Rusoom wa Quyood: Syed Ali Naqi Naqvi Sahab t.s.
Hamare Rusoom wa Quyood: Syed Ali Naqi Naqvi Sahab t.s.Jamal Mirza
 
Samit Malkani_TiE Institute_Social Media Content Strategy_280712
Samit Malkani_TiE Institute_Social Media Content Strategy_280712Samit Malkani_TiE Institute_Social Media Content Strategy_280712
Samit Malkani_TiE Institute_Social Media Content Strategy_280712Samit Malkani
 
English curriculum studies 1 - Lecture 1
English curriculum studies 1 - Lecture 1English curriculum studies 1 - Lecture 1
English curriculum studies 1 - Lecture 1DET
 
Main Bitiya Rani Hoon Na
Main Bitiya Rani Hoon NaMain Bitiya Rani Hoon Na
Main Bitiya Rani Hoon NaSunil Kakkar
 
Tori.fi ostoaie ja big data
Tori.fi ostoaie ja big dataTori.fi ostoaie ja big data
Tori.fi ostoaie ja big dataJussi Lystimaki
 

Viewers also liked (20)

"Lima una ciudad de reyes"
"Lima una ciudad de reyes""Lima una ciudad de reyes"
"Lima una ciudad de reyes"
 
EtCETra 2k15- Entertainment Quiz Finals
EtCETra 2k15- Entertainment Quiz FinalsEtCETra 2k15- Entertainment Quiz Finals
EtCETra 2k15- Entertainment Quiz Finals
 
Ch06 1
Ch06 1Ch06 1
Ch06 1
 
S.S.C. Certificate0001
S.S.C. Certificate0001S.S.C. Certificate0001
S.S.C. Certificate0001
 
English teachers who blog
English teachers who blogEnglish teachers who blog
English teachers who blog
 
Software Radio Implementation: A Systems Perspective
Software Radio Implementation: A Systems PerspectiveSoftware Radio Implementation: A Systems Perspective
Software Radio Implementation: A Systems Perspective
 
olajide doc.PDF
olajide doc.PDFolajide doc.PDF
olajide doc.PDF
 
Bharti B'Day Slide
Bharti B'Day SlideBharti B'Day Slide
Bharti B'Day Slide
 
Raman2
Raman2Raman2
Raman2
 
Expectation of Corporates from Professionals
Expectation of Corporates from ProfessionalsExpectation of Corporates from Professionals
Expectation of Corporates from Professionals
 
Office
OfficeOffice
Office
 
Learning from games : Dr Joanne O'Mara
Learning from games : Dr Joanne O'MaraLearning from games : Dr Joanne O'Mara
Learning from games : Dr Joanne O'Mara
 
Social Media in the Classroom - ICTEV Conf Gail Casey May12
Social Media in the Classroom - ICTEV Conf Gail Casey May12Social Media in the Classroom - ICTEV Conf Gail Casey May12
Social Media in the Classroom - ICTEV Conf Gail Casey May12
 
AATE handout
AATE handoutAATE handout
AATE handout
 
Hamare Rusoom wa Quyood: Syed Ali Naqi Naqvi Sahab t.s.
Hamare Rusoom wa Quyood: Syed Ali Naqi Naqvi Sahab t.s.Hamare Rusoom wa Quyood: Syed Ali Naqi Naqvi Sahab t.s.
Hamare Rusoom wa Quyood: Syed Ali Naqi Naqvi Sahab t.s.
 
Samit Malkani_TiE Institute_Social Media Content Strategy_280712
Samit Malkani_TiE Institute_Social Media Content Strategy_280712Samit Malkani_TiE Institute_Social Media Content Strategy_280712
Samit Malkani_TiE Institute_Social Media Content Strategy_280712
 
Aradhana
AradhanaAradhana
Aradhana
 
English curriculum studies 1 - Lecture 1
English curriculum studies 1 - Lecture 1English curriculum studies 1 - Lecture 1
English curriculum studies 1 - Lecture 1
 
Main Bitiya Rani Hoon Na
Main Bitiya Rani Hoon NaMain Bitiya Rani Hoon Na
Main Bitiya Rani Hoon Na
 
Tori.fi ostoaie ja big data
Tori.fi ostoaie ja big dataTori.fi ostoaie ja big data
Tori.fi ostoaie ja big data
 

More from Philip Tellis

Improving D3 Performance with CANVAS and other Hacks
Improving D3 Performance with CANVAS and other HacksImproving D3 Performance with CANVAS and other Hacks
Improving D3 Performance with CANVAS and other HacksPhilip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonFrontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonPhilip Tellis
 
Frontend Performance: De débutant à Expert à Fou Furieux
Frontend Performance: De débutant à Expert à Fou FurieuxFrontend Performance: De débutant à Expert à Fou Furieux
Frontend Performance: De débutant à Expert à Fou FurieuxPhilip Tellis
 
Frontend Performance: Expert to Crazy Person
Frontend Performance: Expert to Crazy PersonFrontend Performance: Expert to Crazy Person
Frontend Performance: Expert to Crazy PersonPhilip Tellis
 
Beyond Page Level Metrics
Beyond Page Level MetricsBeyond Page Level Metrics
Beyond Page Level MetricsPhilip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...
Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...
Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...Philip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonFrontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonPhilip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonFrontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonPhilip Tellis
 
Frontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonFrontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonPhilip Tellis
 
RUM Distillation 101 -- Part I
RUM Distillation 101 -- Part IRUM Distillation 101 -- Part I
RUM Distillation 101 -- Part IPhilip Tellis
 
Improving 3rd Party Script Performance With IFrames
Improving 3rd Party Script Performance With IFramesImproving 3rd Party Script Performance With IFrames
Improving 3rd Party Script Performance With IFramesPhilip Tellis
 
Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"
Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"
Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"Philip Tellis
 
The Statistics of Web Performance Analysis
The Statistics of Web Performance AnalysisThe Statistics of Web Performance Analysis
The Statistics of Web Performance AnalysisPhilip Tellis
 
Abusing JavaScript to Measure Web Performance
Abusing JavaScript to Measure Web PerformanceAbusing JavaScript to Measure Web Performance
Abusing JavaScript to Measure Web PerformancePhilip Tellis
 
Analysing network characteristics with JavaScript
Analysing network characteristics with JavaScriptAnalysing network characteristics with JavaScript
Analysing network characteristics with JavaScriptPhilip Tellis
 
A Node.JS bag of goodies for analyzing Web Traffic
A Node.JS bag of goodies for analyzing Web TrafficA Node.JS bag of goodies for analyzing Web Traffic
A Node.JS bag of goodies for analyzing Web TrafficPhilip Tellis
 

More from Philip Tellis (20)

Improving D3 Performance with CANVAS and other Hacks
Improving D3 Performance with CANVAS and other HacksImproving D3 Performance with CANVAS and other Hacks
Improving D3 Performance with CANVAS and other Hacks
 
Frontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonFrontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy Person
 
Frontend Performance: De débutant à Expert à Fou Furieux
Frontend Performance: De débutant à Expert à Fou FurieuxFrontend Performance: De débutant à Expert à Fou Furieux
Frontend Performance: De débutant à Expert à Fou Furieux
 
Frontend Performance: Expert to Crazy Person
Frontend Performance: Expert to Crazy PersonFrontend Performance: Expert to Crazy Person
Frontend Performance: Expert to Crazy Person
 
Beyond Page Level Metrics
Beyond Page Level MetricsBeyond Page Level Metrics
Beyond Page Level Metrics
 
Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...
Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...
Frontend Performance: Beginner to Expert to Crazy Person (San Diego Web Perf ...
 
Frontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonFrontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy Person
 
Frontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonFrontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy Person
 
Frontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy PersonFrontend Performance: Beginner to Expert to Crazy Person
Frontend Performance: Beginner to Expert to Crazy Person
 
mmm... beacons
mmm... beaconsmmm... beacons
mmm... beacons
 
RUM Distillation 101 -- Part I
RUM Distillation 101 -- Part IRUM Distillation 101 -- Part I
RUM Distillation 101 -- Part I
 
Improving 3rd Party Script Performance With IFrames
Improving 3rd Party Script Performance With IFramesImproving 3rd Party Script Performance With IFrames
Improving 3rd Party Script Performance With IFrames
 
Extending Boomerang
Extending BoomerangExtending Boomerang
Extending Boomerang
 
Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"
Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"
Abusing JavaScript to measure Web Performance, or, "how does boomerang work?"
 
The Statistics of Web Performance Analysis
The Statistics of Web Performance AnalysisThe Statistics of Web Performance Analysis
The Statistics of Web Performance Analysis
 
Abusing JavaScript to Measure Web Performance
Abusing JavaScript to Measure Web PerformanceAbusing JavaScript to Measure Web Performance
Abusing JavaScript to Measure Web Performance
 
Rum for Breakfast
Rum for BreakfastRum for Breakfast
Rum for Breakfast
 
Analysing network characteristics with JavaScript
Analysing network characteristics with JavaScriptAnalysing network characteristics with JavaScript
Analysing network characteristics with JavaScript
 
A Node.JS bag of goodies for analyzing Web Traffic
A Node.JS bag of goodies for analyzing Web TrafficA Node.JS bag of goodies for analyzing Web Traffic
A Node.JS bag of goodies for analyzing Web Traffic
 
Input sanitization
Input sanitizationInput sanitization
Input sanitization
 

Recently uploaded

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 

Recently uploaded (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 

Regular Expressions Demystified

  • 1. Introduction Diving In Building up /regular expressions/demystified From deckhand to pirate in 30 minutes Philip Tellis / philip@bluesmoon.info Yahoo! /regular expressions/demystified
  • 2. Introduction Diving In Building up Outline 1 Introduction Who’s playing? Conventions 2 Diving In Starting Small Getting meta 3 Building up More or less Alternation Groups /regular expressions/demystified
  • 3. Introduction Who’s playing? Diving In Conventions Building up $ whoami? Philip Tellis philip@bluesmoon.info @bluesmoon yahoo geek /regular expressions/demystified
  • 4. Introduction Who’s playing? Diving In Conventions Building up Who are you? Developer Curious Interested in regular expressions You may or may not have used them before /regular expressions/demystified
  • 5. Introduction Who’s playing? Diving In Conventions Building up What is a regular expression? A pattern that can match multiple strings A pattern matching language A Finite Automaton /regular expressions/demystified
  • 6. Introduction Who’s playing? Diving In Conventions Building up What is a regular expression? But this is a hacker session, so let’s forget the theory. (You can read the book later.) /regular expressions/demystified
  • 7. Introduction Who’s playing? Diving In Conventions Building up What is a regular expression? But this is a hacker session, so let’s forget the theory. (You can read the book later.) /regular expressions/demystified
  • 8. Introduction Who’s playing? Diving In Conventions Building up Conventions used in this talk Text in ’single quotes’ denotes a literal string Text in /forward slashes/ denotes a regular expression The operator =∼ indicates that the string on the left matches the pattern on the right The operator !∼ indicates that the string on the left does not match the pattern on the right $string denotes a variable containing a string /regular expressions/demystified
  • 9. Introduction Starting Small Diving In Getting meta Building up Match a single character ’a’ =~ /a/ /regular expressions/demystified
  • 10. Introduction Starting Small Diving In Getting meta Building up Let’s try a different character ’t’ =~ /t/ /regular expressions/demystified
  • 11. Introduction Starting Small Diving In Getting meta Building up Building up Combine the previous two into a single regular expression ’at’ =~ /at/ /regular expressions/demystified
  • 12. Introduction Starting Small Diving In Getting meta Building up You now know regular expressions To build a regular expression, break the pattern into small manageable pieces and incrementally combine them. /regular expressions/demystified
  • 13. Introduction Starting Small Diving In Getting meta Building up Metacharacters The regex language has its own syntax characters to do funky things Some of these act as wild cards Others act as modifiers to whatever comes before them And some of them make your brain explode We won’t be blowing up brains today /regular expressions/demystified
  • 14. Introduction Starting Small Diving In Getting meta Building up Metacharacters The regex language has its own syntax characters to do funky things Some of these act as wild cards Others act as modifiers to whatever comes before them And some of them make your brain explode We won’t be blowing up brains today /regular expressions/demystified
  • 15. Introduction Starting Small Diving In Getting meta Building up Metacharacters The regex language has its own syntax characters to do funky things Some of these act as wild cards Others act as modifiers to whatever comes before them And some of them make your brain explode We won’t be blowing up brains today /regular expressions/demystified
  • 16. Introduction Starting Small Diving In Getting meta Building up The . metacharacter Matches ONE and ONLY ONE character ’a’ =~ /./ ’b’ =~ /./ ’c’ =~ /./ ’’ !~ /./ The empty string has less than ONE character ’abc’ has ONE character. . . three times ’abc’ =~ /./ /regular expressions/demystified
  • 17. Introduction Starting Small Diving In Getting meta Building up The . metacharacter Matches ONE and ONLY ONE character ’a’ =~ /./ ’b’ =~ /./ ’c’ =~ /./ ’’ !~ /./ The empty string has less than ONE character ’abc’ has ONE character. . . three times ’abc’ =~ /./ /regular expressions/demystified
  • 18. Introduction Starting Small Diving In Getting meta Building up The . metacharacter Matches ONE and ONLY ONE character ’a’ =~ /./ ’b’ =~ /./ ’c’ =~ /./ ’’ !~ /./ The empty string has less than ONE character ’abc’ has ONE character. . . three times ’abc’ =~ /./ /regular expressions/demystified
  • 19. Introduction Starting Small Diving In Getting meta Building up The fate of gate hate date /.ate/ Matches Does not match aate bate cate date . . . ate crates abates dates elated ates ated ... @ate 9ate ’ ate’ /regular expressions/demystified
  • 20. Introduction Starting Small Diving In Getting meta Building up The fate of gate hate date /.ate/ Matches Does not match aate bate cate date . . . ate crates abates dates elated ates ated ... @ate 9ate ’ ate’ /regular expressions/demystified
  • 21. Introduction Starting Small Diving In Getting meta Building up Character classes /[a-z]ate/ Matches Does not match aate bate cate date . . . ate crates abates dates elated ates ated ... @ate 9ate ’ ate’ /regular expressions/demystified
  • 22. Introduction Starting Small Diving In Getting meta Building up Character classes To match a literal ’-’ it should be the first or last character in the class: /[+-*/]/ # Incorrect /[+*/-]/ # Correct /regular expressions/demystified
  • 23. Introduction Starting Small Diving In Getting meta Building up Negated character classes /[^a-z]ate/ Matches Does not match @ate 9ate ’ ate’ ate ates ated g@ate e9ated aate bate cate date . . . crates abates dates elated ... /regular expressions/demystified
  • 24. Introduction Starting Small Diving In Getting meta Building up The late fate of gate hate date rate /[df-hlr]ate/ Matches Does not match date fate gate hate late ate aate bate cate eate rate iate jate kate . . . dates fated billgates hated ... /regular expressions/demystified
  • 25. Introduction Starting Small Diving In Getting meta Building up The late fate of gate hate date rate /[df-hlr]ate/ Matches Does not match date fate gate hate late ate aate bate cate eate rate iate jate kate . . . dates fated billgates hated ... /regular expressions/demystified
  • 26. Introduction Starting Small Diving In Getting meta Building up Anchors /^[df-hlr]ate$/ Matches Does not match date fate gate hate late ate aate bate . . . rate dates gated berate elated ... /regular expressions/demystified
  • 27. Introduction Starting Small Diving In Getting meta Building up Anchors ˆ matches the start of the string $ matches the end of the string Both are 0 byte matches, ie, they do not match any character /regular expressions/demystified
  • 28. Introduction More or less Diving In Alternation Building up Groups Matching more than one of something ? – matches 0 or 1 of what comes before it * – matches 0 or more of what comes before it + – matches 1 or more of what comes before it {n,m} – matches between n and m of what comes before it /regular expressions/demystified
  • 29. Introduction More or less Diving In Alternation Building up Groups Aaargh! Everyone shout “Aaarrrgh!” /regular expressions/demystified
  • 30. Introduction More or less Diving In Alternation Building up Groups How many ways can you say Aargh!? argh aaaaaargh aaaarrrrghhh aaaaarrrrrggggghhhh aaarrrrggggg aaaaarrrrrhhhh /regular expressions/demystified
  • 31. Introduction More or less Diving In Alternation Building up Groups Match ’em all /a+r+g+h+/ # aarrrrgggghhhh /a+r+g+h*/ # aarrgghh & aarrgg /a+r+g*h+/ # aarrgghh & aarrhh /a+r+g*h*/ # argh & arg & arh That last one also matches ’ar’ which we don’t want /regular expressions/demystified
  • 32. Introduction More or less Diving In Alternation Building up Groups Match ’em all /a+r+g+h+/ # aarrrrgggghhhh /a+r+g+h*/ # aarrgghh & aarrgg /a+r+g*h+/ # aarrgghh & aarrhh /a+r+g*h*/ # argh & arg & arh That last one also matches ’ar’ which we don’t want /regular expressions/demystified
  • 33. Introduction More or less Diving In Alternation Building up Groups Match ’em all /a+r+g+h+/ # aarrrrgggghhhh /a+r+g+h*/ # aarrgghh & aarrgg /a+r+g*h+/ # aarrgghh & aarrhh /a+r+g*h*/ # argh & arg & arh That last one also matches ’ar’ which we don’t want /regular expressions/demystified
  • 34. Introduction More or less Diving In Alternation Building up Groups Match ’em all /a+r+g+h+/ # aarrrrgggghhhh /a+r+g+h*/ # aarrgghh & aarrgg /a+r+g*h+/ # aarrgghh & aarrhh /a+r+g*h*/ # argh & arg & arh That last one also matches ’ar’ which we don’t want /regular expressions/demystified
  • 35. Introduction More or less Diving In Alternation Building up Groups Match ’em all /a+r+g+h+/ # aarrrrgggghhhh /a+r+g+h*/ # aarrgghh & aarrgg /a+r+g*h+/ # aarrgghh & aarrhh /a+r+g*h*/ # argh & arg & arh That last one also matches ’ar’ which we don’t want /regular expressions/demystified
  • 36. Introduction More or less Diving In Alternation Building up Groups Alternation: Match all this or all that /ab|cd/ Matches either ’ab’ or ’cd’ /regular expressions/demystified
  • 37. Introduction More or less Diving In Alternation Building up Groups From here to eternity | matches either everything on its left or everything on its right (That’s a pipe character, not the letter I) /regular expressions/demystified
  • 38. Introduction More or less Diving In Alternation Building up Groups Back to aaargh /g*h+|g+h*/ This matches all the endings we want: ggggghhhhhh ggggg hhhhh /regular expressions/demystified
  • 39. Introduction More or less Diving In Alternation Building up Groups Back to aaargh /a+r+g*h+|g+h*/ This doesn’t quite work Matches Does not match aaarrrhhh aaarrrggg aaarrrrggghhh gggg gggghhhh /regular expressions/demystified
  • 40. Introduction More or less Diving In Alternation Building up Groups Back to aaargh /a+r+g*h+|g+h*/ This doesn’t quite work Matches Does not match aaarrrhhh aaarrrggg aaarrrrggghhh gggg gggghhhh /regular expressions/demystified
  • 41. Introduction More or less Diving In Alternation Building up Groups Group the subexpression /a+r+(g*h+|g+h*)/ Matches aaarrrhhh aaarrrggg aaarrrrggghhh /regular expressions/demystified
  • 42. Introduction More or less Diving In Alternation Building up Groups Grouping parentheses ( and ) mark a group | alternates within a group Groups may be nested - it’s like a new regex inside +, *, ? and {n,m} may apply to an entire group /regular expressions/demystified
  • 43. Introduction Diving In Building up Stop /regular expressions/demystified
  • 44. Introduction Diving In Building up Summary Start small, match the parts you understand Build up to more complex patterns Not all problems should be solved by regular expressions /regular expressions/demystified
  • 45. Introduction Diving In Building up More Info. . . “Mastering Regular Expressions” – Jeffrey Friedl http://tech.bluesmoon.info/search/label/regex /regular expressions/demystified
  • 46. Introduction Diving In Building up Contact me Philip Tellis philip@bluesmoon.info @bluesmoon bluesmoon.info /regular expressions/demystified
  • 47. Introduction Diving In Building up Image credits http://flickr.com/photos/practicalowl/3933514241/ http://flickr.com/photos/loozrboy/3908830690/ http://flickr.com/photos/thetruthabout/2680546103/ http://flickr.com/photos/donsolo/2136923757/ /regular expressions/demystified
  • 48. Introduction Diving In Building up Thank You /regular expressions/demystified
  • 49. Introduction Diving In Building up Aargh with class /a+r+g*[gh]h*/ Matches aaarrrhhh aaarrrggg aaarrrrggghhh /regular expressions/demystified
  • 50. Introduction Diving In Building up Matching meta characters in a character class /[a-zA-Z0-9_-]/ /[a-z^]/ /[][]/ /regular expressions/demystified
  • 51. Introduction Diving In Building up Alternating multiple items /apples|oranges|bananas/ /buy some (apples|oranges|ba(na){2}s)/ /regular expressions/demystified