Regular Expressions with Ruby

Regular Expressions with Ruby

What Do You Know? Sydney, 03 April, 2014

492d339a2ec66fa8d80e937abddb58e6?s=128

Elle Meredith

April 03, 2014
Tweet

Transcript

  1. Regular Expressions with Ruby Elle Meredith – @aemeredith

  2. Test Extract Change

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

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

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

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

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

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

    lazy dog” ! matchdata = string.match /fox/ => #<MatchData "fox">
  9. MatchData matchdata.to_s => "fox"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    day:"03" month:"APR" year:"2014">
  27. Named Captures md['day'] => "03"

  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. '''
  29. Look Around string.scan /love my/ => ["love my", "love my”]

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

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

  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"
  33. Changing things string.gsub!(/\she/, ' she').gsub!(/ his/, 'her')

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

  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"
  36. None
  37. Regular Expressions with Ruby Elle Meredith – @aemeredith