0
Introduction
               Diving In
            Building up




/regular expressions/demystified
 From deckhand to pirate...
Introduction
                              Diving In
                           Building up


Outline

  1   Introduction
...
Introduction
                                    Who’s playing?
                        Diving In
                        ...
Introduction
                                        Who’s playing?
                            Diving In
                ...
Introduction
                                        Who’s playing?
                            Diving In
                ...
Introduction
                                         Who’s playing?
                             Diving In
              ...
Introduction
                                         Who’s playing?
                             Diving In
              ...
Introduction
                                         Who’s playing?
                             Diving In
              ...
Introduction
                                     Starting Small
                         Diving In
                      ...
Introduction
                                      Starting Small
                          Diving In
                    ...
Introduction
                                         Starting Small
                             Diving In
              ...
Introduction
                                         Starting Small
                             Diving In
              ...
Introduction
                                       Starting Small
                           Diving In
                  ...
Introduction
                                       Starting Small
                           Diving In
                  ...
Introduction
                                       Starting Small
                           Diving In
                  ...
Introduction
                                       Starting Small
                           Diving In
                  ...
Introduction
                                       Starting Small
                           Diving In
                  ...
Introduction
                                       Starting Small
                           Diving In
                  ...
Introduction
                                            Starting Small
                                Diving In
        ...
Introduction
                                            Starting Small
                                Diving In
        ...
Introduction
                                           Starting Small
                               Diving In
          ...
Introduction
                                          Starting Small
                              Diving In
            ...
Introduction
                                       Starting Small
                           Diving In
                  ...
Introduction
                                          Starting Small
                              Diving In
            ...
Introduction
                                          Starting Small
                              Diving In
            ...
Introduction
                                         Starting Small
                             Diving In
              ...
Introduction
                                        Starting Small
                            Diving In
                ...
Introduction    More or less
                           Diving In   Alternation
                        Building up    Gro...
Introduction    More or less
              Diving In   Alternation
           Building up    Groups


Aaargh!




        ...
Introduction    More or less
                          Diving In   Alternation
                       Building up    Group...
Introduction    More or less
                            Diving In   Alternation
                         Building up    G...
Introduction    More or less
                            Diving In   Alternation
                         Building up    G...
Introduction    More or less
                            Diving In   Alternation
                         Building up    G...
Introduction    More or less
                            Diving In   Alternation
                         Building up    G...
Introduction    More or less
                            Diving In   Alternation
                         Building up    G...
Introduction    More or less
                           Diving In   Alternation
                        Building up    Gro...
Introduction    More or less
                              Diving In   Alternation
                           Building up ...
Introduction    More or less
                            Diving In   Alternation
                         Building up    G...
Introduction    More or less
                                Diving In   Alternation
                             Building...
Introduction    More or less
                                Diving In   Alternation
                             Building...
Introduction    More or less
                          Diving In   Alternation
                       Building up    Group...
Introduction    More or less
                            Diving In   Alternation
                         Building up    G...
Introduction
           Diving In
        Building up


Stop




                       /regular expressions/demystified
Introduction
                          Diving In
                       Building up


Summary




    Start small, match t...
Introduction
                            Diving In
                         Building up


More Info. . .




      “Master...
Introduction
                       Diving In
                    Building up


Contact me




  Philip Tellis
  philip@bl...
Introduction
                             Diving In
                          Building up


Image credits




     http://...
Introduction
    Diving In
 Building up




     Thank You




                /regular expressions/demystified
Introduction
                          Diving In
                       Building up


Aargh with class



      /a+r+g*[gh...
Introduction
                       Diving In
                    Building up


Matching meta characters in a character cl...
Introduction
                        Diving In
                     Building up


Alternating multiple items




       /a...
Upcoming SlideShare
Loading in...5
×

Regular Expressions Demystified

1,991

Published on

Introductory talk on regular expressions for developers who'd like to get their hands dirty. The full tutorial is documented here:
http://tech.bluesmoon.info/2006/04/beginning-regular-expressions.html

Published in: Technology, Sports
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,991
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
48
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Regular Expressions Demystified"

  1. 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. 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. 3. Introduction Who’s playing? Diving In Conventions Building up $ whoami? Philip Tellis philip@bluesmoon.info @bluesmoon yahoo geek /regular expressions/demystified
  4. 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. 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. 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. 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. 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. 9. Introduction Starting Small Diving In Getting meta Building up Match a single character ’a’ =~ /a/ /regular expressions/demystified
  10. 10. Introduction Starting Small Diving In Getting meta Building up Let’s try a different character ’t’ =~ /t/ /regular expressions/demystified
  11. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 29. Introduction More or less Diving In Alternation Building up Groups Aaargh! Everyone shout “Aaarrrgh!” /regular expressions/demystified
  30. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 43. Introduction Diving In Building up Stop /regular expressions/demystified
  44. 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. 45. Introduction Diving In Building up More Info. . . “Mastering Regular Expressions” – Jeffrey Friedl http://tech.bluesmoon.info/search/label/regex /regular expressions/demystified
  46. 46. Introduction Diving In Building up Contact me Philip Tellis philip@bluesmoon.info @bluesmoon bluesmoon.info /regular expressions/demystified
  47. 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. 48. Introduction Diving In Building up Thank You /regular expressions/demystified
  49. 49. Introduction Diving In Building up Aargh with class /a+r+g*[gh]h*/ Matches aaarrrhhh aaarrrggg aaarrrrggghhh /regular expressions/demystified
  50. 50. Introduction Diving In Building up Matching meta characters in a character class /[a-zA-Z0-9_-]/ /[a-z^]/ /[][]/ /regular expressions/demystified
  51. 51. Introduction Diving In Building up Alternating multiple items /apples|oranges|bananas/ /buy some (apples|oranges|ba(na){2}s)/ /regular expressions/demystified
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×