Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Regular Expressions        101       Raj Kissu
What are Regular  Expressions?
They describepatterns in strings
These patterns can be used to modify      strings
Invented by Stephen    Cole Kleene
Idea of RegEx dates back to the 1950s
Today, they come indifferent “flavors”
PCRE, POSIX Basic &Extended RegEx, ECMA  RegEx and loads       more!
NOTE: RegEx“flavors” are not   consistent in  implementation
Different “flavors”       behave  differently ...
So know your“flavor” before you       use it!
Why Use It?
Cos it’s animportant tool in  every coder’s     arsenal!
Ok, but still ...  Why use it???
It’s Short, Sweet &   F***ing Fast!
It can match just about anything!
It makes changing large amounts of  repetitive text    trivial ...
... as long as youcan “see” patterns
It Makes You Awesome- in a Geeky way :)
Before RegEx Mastery
After RegEx Mastery
Readily AvailableSupport in programming languages:JavaScript, PHP, PERL, C/C++,etcCommand-line: grep, awk, sedText-editors...
RegEx BasicsNOTE : Using ECMA (JavaScript) RegEx Flavor!
Charactersstandard characters  letters : A to Z, a to z  numbers : 0 to 9  symbols : !,@,#,%,& etcMatched literally!
Meta CharactersSpecial characters : )(][^$.|?*+To match as literals, escape themwith a backslash
Character ClassesMatches one and ONLY one character ina set of characters[Aa]   : matches either ‘A’ or ‘a’[a-z] : matches...
Character ClassesMetacharacters may behave differentlywithin character classes[^red] : matches anything but ‘r’,‘e’ and ‘d...
Shorthand Classesd, [0-9]: digitsw, [da-zA-Z_]: alphanumeric or _s or [ t(?:n|rn)] : whitespaceD, W, S : the above BUT neg...
The Dot CharacterThe Dot (.) character matches anysingle character BUT the newlineSynonymous with [^n] (UNIX/Linux/Mac)as ...
AlternationUsing a pipe |, match either the leftor right side of the patternbear|tiger : matches a string thatcontains eit...
Quantifiers{n} : matches exactly n times{n,} : matches n or more times{n,m} : matches between n and m times* : same as {0,...
QuantifiersQuantifiers are greedy<.+> : matches “<div>holy RegEx,Batman!</div>” instead of stopping at“<div>”Add a ? to ma...
AnchorsMatches positions instead ofcharacters^ : matches the beginning of a string$ : matches the end of a stringb : match...
GroupingsPlacing parentheses around tokensgroups them together : /nyan(cat)/It also provides abackreference :    /(cat)1/ ...
LookaheadPositive Lookahead  Iron(?=man) : matches “Iron” only  if it is followed by “man”Negative Lookahead  Iron(?!man) ...
LookbehindPositive Lookbehind  (?<=Iron)man : matches “man” only  if it is preceded by “Iron”Negative Lookbehind  (?<!Iron...
Modifiersalter behavior of the matching mode(differs between tools)/i : case-insensitive match/m : Multi-line mode/g : aff...
Q & A
ResourcesMastering Regular Expressions -Jeffrey E.F. Friedlhttp://www.regex.infohttp://www.regular-expressions.infohttp://...
Thank You!
Upcoming SlideShare
Loading in …5
×

Regular Expressions 101

9,326 views

Published on

Published in: Technology
  • Be the first to comment

Regular Expressions 101

  1. 1. Regular Expressions 101 Raj Kissu
  2. 2. What are Regular Expressions?
  3. 3. They describepatterns in strings
  4. 4. These patterns can be used to modify strings
  5. 5. Invented by Stephen Cole Kleene
  6. 6. Idea of RegEx dates back to the 1950s
  7. 7. Today, they come indifferent “flavors”
  8. 8. PCRE, POSIX Basic &Extended RegEx, ECMA RegEx and loads more!
  9. 9. NOTE: RegEx“flavors” are not consistent in implementation
  10. 10. Different “flavors” behave differently ...
  11. 11. So know your“flavor” before you use it!
  12. 12. Why Use It?
  13. 13. Cos it’s animportant tool in every coder’s arsenal!
  14. 14. Ok, but still ... Why use it???
  15. 15. It’s Short, Sweet & F***ing Fast!
  16. 16. It can match just about anything!
  17. 17. It makes changing large amounts of repetitive text trivial ...
  18. 18. ... as long as youcan “see” patterns
  19. 19. It Makes You Awesome- in a Geeky way :)
  20. 20. Before RegEx Mastery
  21. 21. After RegEx Mastery
  22. 22. Readily AvailableSupport in programming languages:JavaScript, PHP, PERL, C/C++,etcCommand-line: grep, awk, sedText-editors: VIM, emacs, Notepad++IDEs: Aptana, Netbeans, VisualStudio .NET
  23. 23. RegEx BasicsNOTE : Using ECMA (JavaScript) RegEx Flavor!
  24. 24. Charactersstandard characters letters : A to Z, a to z numbers : 0 to 9 symbols : !,@,#,%,& etcMatched literally!
  25. 25. Meta CharactersSpecial characters : )(][^$.|?*+To match as literals, escape themwith a backslash
  26. 26. Character ClassesMatches one and ONLY one character ina set of characters[Aa] : matches either ‘A’ or ‘a’[a-z] : matches any of the lowercasealphabets in the specified range ONCE[^Aa] : matches anything but ‘A’ and‘a’
  27. 27. Character ClassesMetacharacters may behave differentlywithin character classes[^red] : matches anything but ‘r’,‘e’ and ‘d’[r^ed] : matches only ‘r’, ‘^’, ‘e’or ‘d’
  28. 28. Shorthand Classesd, [0-9]: digitsw, [da-zA-Z_]: alphanumeric or _s or [ t(?:n|rn)] : whitespaceD, W, S : the above BUT negated
  29. 29. The Dot CharacterThe Dot (.) character matches anysingle character BUT the newlineSynonymous with [^n] (UNIX/Linux/Mac)as well as [^rn] (Windows)Use it sparingly - it’s expensive
  30. 30. AlternationUsing a pipe |, match either the leftor right side of the patternbear|tiger : matches a string thatcontains either “bear” or “tiger”pedo(bea|tige)r : matches a stringthat contains either “pedobear” or“pedotiger”
  31. 31. Quantifiers{n} : matches exactly n times{n,} : matches n or more times{n,m} : matches between n and m times* : same as {0,}+ : same as {1,}? : same as {0,1}
  32. 32. QuantifiersQuantifiers are greedy<.+> : matches “<div>holy RegEx,Batman!</div>” instead of stopping at“<div>”Add a ? to make it lazy<.+?> : stops at “<div>” in“<div>holy regex!</div>”
  33. 33. AnchorsMatches positions instead ofcharacters^ : matches the beginning of a string$ : matches the end of a stringb : matches between a w and a tokenthat’s not a w
  34. 34. GroupingsPlacing parentheses around tokensgroups them together : /nyan(cat)/It also provides abackreference : /(cat)1/ matches“cat”OR if you don’t want abackreference : /(?:nyan)(cat)1/matches “nyancatcat” and not“nyancatnyan”
  35. 35. LookaheadPositive Lookahead Iron(?=man) : matches “Iron” only if it is followed by “man”Negative Lookahead Iron(?!man) : matches “Iron” only if it is not followed by “man”
  36. 36. LookbehindPositive Lookbehind (?<=Iron)man : matches “man” only if it is preceded by “Iron”Negative Lookbehind (?<!Iron)man : matches “man” only if it is not preceded by “Iron”
  37. 37. Modifiersalter behavior of the matching mode(differs between tools)/i : case-insensitive match/m : Multi-line mode/g : affects all possible matches,not just the first
  38. 38. Q & A
  39. 39. ResourcesMastering Regular Expressions -Jeffrey E.F. Friedlhttp://www.regex.infohttp://www.regular-expressions.infohttp://www.rubular.com
  40. 40. Thank You!

×