Fear Me Not

259f23c3b129f07b0c496b9f0495f07e?s=47 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.

259f23c3b129f07b0c496b9f0495f07e?s=128

jeg2

March 14, 2009
Tweet

Transcript

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

    for Conquering Regular Expressions
  2. Contrary to popular opinion, Regex doesn’t have to make you

    feel like this => Popular Opinion
  3. Do I need help?

  4. Do I need help? My name is Dana and I

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

    like Regular Expressions It’s been two days since my last Regular Expression
  6. When is the end  really the end?

  7. When is the end  really the end? data[/\d+$/] #

    => "10"
  8. When is the end  really the end? data[/\d+$/] #

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

    => "10" data[/\d+\z/] # => nil attendance = "Women = 10 Men = 215 Total = 225 "
  10. 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 "
  11. In Ruby dummy talk, please

  12. In Ruby dummy talk, please Symbol WTF?

  13. In Ruby dummy talk, please Symbol WTF? ^ Matches the

    beginning of any line - 100 lines means 100 matches
  14. 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
  15. 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
  16. 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
  17. 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
  18. Getting exactly  what I want

  19. 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."]
  20. 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
  21. 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
  22. 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."]
  23. Let’s discuss our differences What’s the difference between this -

    ( ... ) ! ! and this? - (?: ... )
  24. 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
  25. 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
  26. When you can’t make up your mind Sometimes you just

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

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

    can’t decide exactly what you want /(42|41|40|...)/ /[0-9]/
  29. 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]/
  30. 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])/
  31. None
  32. "Fear Regex".sub!(/\AFear\b/, "Embrace")