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

    View full-size slide

  2. Contrary to popular
    opinion, Regex
    doesn’t have to make
    you feel like this =>
    Popular Opinion

    View full-size slide

  3. Do I need help?

    View full-size slide

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

    View full-size slide

  5. Do I need help?
    My name is Dana and I like Regular Expressions
    It’s been two days since my last Regular
    Expression

    View full-size slide

  6. When is the end
    really the end?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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
    "

    View full-size slide

  11. In Ruby dummy talk, please

    View full-size slide

  12. In Ruby dummy talk, please
    Symbol WTF?

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  18. Getting exactly
    what I want

    View full-size slide

  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."]

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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."]

    View full-size slide

  23. Let’s discuss our differences
    What’s the difference
    between this - ( ... )
    !
    !
    and this? - (?: ... )

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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]/

    View full-size slide

  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])/

    View full-size slide

  31. "Fear Regex".sub!(/\AFear\b/, "Embrace")

    View full-size slide