Regular Expressions 101

7,866 views

Published on

Published in: Technology
0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,866
On SlideShare
0
From Embeds
0
Number of Embeds
1,356
Actions
Shares
0
Downloads
306
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 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!

    ×