Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Regular Expressions Are Your Friend

Regular Expressions Are Your Friend

This was a talk given by me at WindyCityRails, its a combined talk with choices to do the basics or advanced. The audience chose the advanced side of the talk.

Regular Expressions help you find that needle in the haystack, where Waldo might be, or the location of Jimmy Hoffa’s body. You’ll find them in most programming languages and tools, but they can do so much more than checking an e-mail address! We’ll go over the basics as well as advanced topics and you’re encouraged to follow along as the examples are shown.

Video here: http://vimeo.com/31204910

Aaron Kalin

October 02, 2011
Tweet

More Decks by Aaron Kalin

Other Decks in Programming

Transcript

  1. Quantifiers * (Zero or more) + (One or more) ?

    (One or None) {n,m} (Only n matches up to m)
  2. Quantifiers (part Deux) Changes the number of matches to the

    left of the quantifier. a* (if there, match a, keep going) a+ (must match a, keep going) a? (if there, match a) a{1,3} (matches a or aa or aaa)
  3. Character Classes (gotcha) [.+*] (Matches a dot, plus or splat)

    Any meta character inside a character class becomes a literal character
  4. Character Classes (part Deux) \w = [A-Za-z0-9_] \W = [^A-Za-z0-9_]

    \s = [ \t\r\n] The - is a range of literal characters
  5. Grouping Combine matches into groups using ( and ) surrounding

    your matches. (Hello) World Matches Hello World and Hello separately
  6. Escaping (from alcatraz) Use \ to turn a metacharacter into

    a literal . Matches anything \. Matches a period
  7. /^Hello [Ww]orld.?$/ Beginning End Exact letters H e l l

    o (including space) Exact letters o r l d Anything Maybe once? Capital or lower-case W
  8. /^Hello [Ww]orld.?$/ Beginning End Exact letters H e l l

    o (including space) Exact letters o r l d Anything Maybe once? Capital or lower-case W
  9. Precision vs Greed Be as specific as you can! Avoid

    use of the dot . Avoid greedy quantifiers + or .+
  10. Options Matching options (usually added after the second /) i

    - case insensitive o - interpolate once (#{}’s) m - multiline mode (‘.’ matches \n) x - extended mode (ignore whitespace)
  11. Assertions (?=) Look Ahead (?!) Negative Look Ahead (?<) Look

    Behind (?<!) Negative Look Behind (?(if) then | else) Conditional
  12. Follow Along RubyXP (Ruby 1.9 Regex Tester) http://www.rubyxp.com/ Graffiti (RubyGem)

    http://rubygems.org/gems/graffiti gem install graffiti
  13. /^Hello [Ww]orld.?$/ Beginning End Exact letters H e l l

    o (including space) Exact letters o r l d Anything Maybe once? Capital or lower-case W
  14. Quantifiers * (Zero or more) + (One or more) ?

    (One or None) {n,m} (Only n matches up to m)
  15. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more Followed by non-number 1000000.00
  16. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1000000.00 Followed by non-number
  17. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1000000.00 Followed by non-number
  18. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1000000.00 Followed by non-number
  19. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  20. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  21. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  22. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  23. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number FALSE
  24. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  25. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  26. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number FALSE
  27. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  28. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  29. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  30. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000000.00 Followed by non-number
  31. /(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers

    ahead at least once or more 1,000,000.00 Followed by non-number
  32. Thank you! Windy City Rails Organizers! Derek Weathersbee (Franchise Font)

    http://vurl.me/AZSG Raph Levien (Inconsolata Font) http://vurl.me/AZSH