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 Slide

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

    View Slide

  3. Do I need help?

    View Slide

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

    View 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 Slide

  6. When is the end
    really the end?

    View Slide

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

    View Slide

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

    View Slide

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

    View 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 Slide

  11. In Ruby dummy talk, please

    View Slide

  12. In Ruby dummy talk, please
    Symbol WTF?

    View Slide

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

    View 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 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 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 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 Slide

  18. Getting exactly
    what I want

    View 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 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 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 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 Slide

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

    View 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 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 Slide

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

    View Slide

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

    View 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 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 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 Slide

  31. View Slide

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

    View Slide