Regular Expressions Demystified
Upcoming SlideShare
Loading in...5
×
 

Regular Expressions Demystified

on

  • 2,827 views

Introductory talk on regular expressions for developers who'd like to get their hands dirty. The full tutorial is documented here:

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

Statistics

Views

Total Views
2,827
Views on SlideShare
2,727
Embed Views
100

Actions

Likes
3
Downloads
45
Comments
0

4 Embeds 100

http://talks.bluesmoon.info 67
http://www.linkedin.com 20
http://www.slideshare.net 7
https://www.linkedin.com 6

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Regular Expressions Demystified Regular Expressions Demystified Presentation Transcript

  • 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