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