#171 What the RegEx?
Summary
In this episode, we look at different ways to use pattern matching with Regular Expressions in Ruby.ruby 8:22
2-3-2019
Resources
RegExr - https://regexr.com
Cheatsheet - https://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
Summary
rubyVALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
puts '[email protected]'.match?(VALID_EMAIL_REGEX)
puts '@example.com'.match?(VALID_EMAIL_REGEX)
puts '[email protected]' =~ VALID_EMAIL_REGEX
puts ('@example.com' =~ VALID_EMAIL_REGEX).nil?
puts /[a-z]/.match('123456').nil?
puts /[a-z]/.match('hijklm')
puts URI::MailTo::EMAIL_REGEXP
puts '[email protected]'.match?(URI::MailTo::EMAIL_REGEXP)
puts '@example.com'.match?(URI::MailTo::EMAIL_REGEXP)
string = "HELLOWORLD".downcase
case string
when /^[a-z]*$/
puts 'lower case'
when /^[A-Z]*$/
puts 'upper case'
else
puts 'mixed case'
end
string = 'hello-this-IS_a_test'
delimiters = ['-', '_']
puts string.split(Regexp.union(delimiters))
puts "#{string.split(Regexp.union(delimiters))}"
words = %w[happier unicorn newer mermaid pony]
puts words.grep(/er\z/)
puts "A bad word is frick".sub(/frick/, '*****')
Worth mentioning: https://rubular.com/