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

Fear Me Not

jeg2
March 14, 2009

Fear Me Not

This was a lightning talk given by my wife, Dana Gray, at MountainWest RubyConf 2009. She encouraged programmers to shed their fear of regular expressions and learn to use them properly.

jeg2

March 14, 2009
Tweet

More Decks by jeg2

Other Decks in Technology

Transcript

  1. Don’t worry, there are only four. Fear Me Not Tips

    for Conquering Regular Expressions
  2. Do I need help? My name is Dana and I

    like Regular Expressions
  3. Do I need help? My name is Dana and I

    like Regular Expressions It’s been two days since my last Regular Expression
  4. When is the end  really the end? data[/\d+$/] #

    => "10" attendance = "Women = 10 Men = 215 Total = 225 "
  5. When is the end  really the end? data[/\d+$/] #

    => "10" data[/\d+\z/] # => nil attendance = "Women = 10 Men = 215 Total = 225 "
  6. When is the end  really the end? data[/\d+$/] #

    => "10" data[/\d+\z/] # => nil data[/\d+\Z/] # => "225" attendance = "Women = 10 Men = 215 Total = 225 "
  7. In Ruby dummy talk, please Symbol WTF? ^ Matches the

    beginning of any line - 100 lines means 100 matches
  8. In Ruby dummy talk, please Symbol WTF? ^ Matches the

    beginning of any line - 100 lines means 100 matches \A Matches the beginning of the string - at most 1 match
  9. In Ruby dummy talk, please Symbol WTF? ^ Matches the

    beginning of any line - 100 lines means 100 matches \A Matches the beginning of the string - at most 1 match $ Matches the end of any line - 100 lines means 100 matches
  10. In Ruby dummy talk, please Symbol WTF? ^ Matches the

    beginning of any line - 100 lines means 100 matches \A Matches the beginning of the string - at most 1 match $ Matches the end of any line - 100 lines means 100 matches \z Matches the end of the string - at most 1 match
  11. In Ruby dummy talk, please Symbol WTF? ^ Matches the

    beginning of any line - 100 lines means 100 matches \A Matches the beginning of the string - at most 1 match $ Matches the end of any line - 100 lines means 100 matches \z Matches the end of the string - at most 1 match \Z Matches the end of the string or just before the last \n - at most 1 match
  12. Getting exactly  what I want ["Gray is a great

    color.", "I like gray things.", "I’m hung up on a guy named Gray.", "He lives in Castle Grayskull."]
  13. Getting exactly  what I want ["Gray is a great

    color.", "I like gray things.", "I’m hung up on a guy named Gray.", "He lives in Castle Grayskull."] /(\W|\A)gray(\W|\Z)/i
  14. Getting exactly  what I want ["Gray is a great

    color.", "I like gray things.", "I’m hung up on a guy named Gray.", "He lives in Castle Grayskull."] /(\W|\A)gray(\W|\Z)/i /\bGray\b/i
  15. Getting exactly  what I want ["Gray is a great

    color.", "I like gray things.", "I’m hung up on a guy named Gray.", "He lives in Castle Grayskull."] /(\W|\A)gray(\W|\Z)/i /\bGray\b/i ["Gray is a great color.", "I like gray things.", "I’m hung up on a guy named Gray."]
  16. Let’s discuss our differences What’s the difference between this -

    ( ... ) ! ! and this? - (?: ... ) phone = "123-456-7890" phone =~ /\A(\d+)-(\d+)-(\d+)\z/ puts "#{$1}#{$2}#{$3}" ! # >> 1234567890
  17. Let’s discuss our differences What’s the difference between this -

    ( ... ) ! ! and this? - (?: ... ) phone = "123-456-7890" phone =~ /\A(\d+)-(\d+)-(\d+)\z/ puts "#{$1}#{$2}#{$3}" ! # >> 1234567890 name = "James Edward Gray II" name =~ /\A(\w+)(?:\s\w+)?\s(\w+)/ puts "#{$1} #{$2}" ! # >> James Gray
  18. When you can’t make up your mind Sometimes you just

    can’t decide exactly what you want
  19. When you can’t make up your mind Sometimes you just

    can’t decide exactly what you want /(42|41|40|...)/
  20. When you can’t make up your mind Sometimes you just

    can’t decide exactly what you want /(42|41|40|...)/ /[0-9]/
  21. When you can’t make up your mind Sometimes you just

    can’t decide exactly what you want /(42|41|40|...)/ /[0-9]/ /[1-3]?[0-9]/
  22. When you can’t make up your mind Sometimes you just

    can’t decide exactly what you want /(42|41|40|...)/ /[0-9]/ /[1-3]?[0-9]/ /(4[0-2]|[1-3]?[0-9])/