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

Regular Expressions with Ruby

Regular Expressions with Ruby

What Do You Know? Sydney, 03 April, 2014

Elle Meredith

April 03, 2014
Tweet

More Decks by Elle Meredith

Other Decks in Technology

Transcript

  1. Regular Expressions
    with Ruby
    Elle Meredith – @aemeredith

    View Slide

  2. Test
    Extract
    Change

    View Slide

  3. /fox/ =~ "The quick brown fox"
    Basic Matching

    View Slide

  4. /fox/ =~ "The quick brown fox"
    => 16
    Basic Matching

    View Slide

  5. /cat/ =~ "The quick brown fox"
    => nil
    Basic Matching

    View Slide

  6. /cat/ !~ "The quick brown fox"
    => true
    Basic Matching

    View Slide

  7. MatchData
    string = "The quick brown fox
    jumps over the lazy dog”

    View Slide

  8. MatchData
    string = "The quick brown fox
    jumps over the lazy dog”
    !
    matchdata = string.match /fox/
    => #

    View Slide

  9. MatchData
    matchdata.to_s
    => "fox"

    View Slide

  10. MatchData
    matchdata.pre_match
    => "The quick brown "

    View Slide

  11. MatchData
    matchdata.post_match
    => " jumps over the lazy dog"

    View Slide

  12. Captures
    string = "03APR2014"
    !
    string.match /\D{3}/

    View Slide

  13. Captures
    string = "03APR2014"
    !
    string.match /\D{3}/

    View Slide

  14. Captures
    string = "03APR2014"
    !
    string.match /\D{3}/

    View Slide

  15. Captures
    string = "03APR2014"
    !
    string.match /\D{3}/
    => #

    View Slide

  16. Captures
    md = string.match /(.*)(\D{3})(.*)/

    View Slide

  17. Captures
    md = string.match /(.*)(\D{3})(.*)/

    View Slide

  18. Captures
    md = string.match /(.*)(\D{3})(.*)/

    View Slide

  19. Captures
    md = string.match /(.*)(\D{3})(.*)/
    => #1:"03" 2:"APR" 3:"2014">

    View Slide

  20. Captures
    md.captures
    => ["03", "APR", "2014"]

    View Slide

  21. Captures
    md = string.match /(.*)(\D{3})(.*)/
    => #1:"03" 2:"APR" 3:"2014">

    View Slide

  22. Captures
    md[1]
    => "03"
    md[2]
    => "APR"
    md[3]
    => "2014"

    View Slide

  23. Named Captures
    md = string.match /(?.*)(?
    \D{3})(?.*)/
    => #day:"03" month:"APR"
    year:"2014">

    View Slide

  24. Named Captures
    md = string.match /(?.*)(?
    \D{3})(?.*)/
    => #day:"03" month:"APR"
    year:"2014">

    View Slide

  25. Named Captures
    md = string.match /(?.*)(?
    \D{3})(?.*)/
    => #day:"03" month:"APR"
    year:"2014">

    View Slide

  26. Named Captures
    md = string.match /(?.*)(?
    \D{3})(?.*)/
    => #day:"03" month:"APR"
    year:"2014">

    View Slide

  27. Named Captures
    md['day']
    => "03"

    View Slide

  28. Look Around
    string = '''
    I love my job, I love the pay!
    I love it more and more each day.
    I love my boss, he is the best!
    I love his boss and all the rest.
    '''

    View Slide

  29. Look Around
    string.scan /love my/
    => ["love my", "love my”]

    View Slide

  30. Positive Lookahead
    string.scan /love my (?=job)/
    => ["love my "]

    View Slide

  31. Changing things
    string.gsub(/love/, 'hate')

    View Slide

  32. Changing things
    string.gsub(/love/, 'hate')
    => "\nI hate my job, I hate the pay!
    \nI hate it more and more each
    day.\nI hate my boss, he is the
    best!\nI hate his boss and all the
    rest.\n"

    View Slide

  33. Changing things
    string.gsub!(/\she/, ' she').gsub!(/
    his/, 'her')

    View Slide

  34. Changing things
    string.gsub!(/\she/, ' she').gsub!(/
    his/, 'her')

    View Slide

  35. Changing things
    string.gsub!(/\she/, ' she').gsub!(/
    his/, ‘her')
    => "\nI love my job, I love the pay!
    \nI love it more and more each day.
    \nI love my boss, she is the best!\nI
    love her boss and all the rest.\n"

    View Slide

  36. View Slide

  37. Regular Expressions
    with Ruby
    Elle Meredith – @aemeredith

    View Slide