Skip to content

Instantly share code, notes, and snippets.

@CheezItMan
Created March 12, 2019 18:24
Show Gist options
  • Save CheezItMan/2c40031c021eacc891aaa03742ba0b6e to your computer and use it in GitHub Desktop.
Save CheezItMan/2c40031c021eacc891aaa03742ba0b6e to your computer and use it in GitHub Desktop.
phone_numbers = [
"(206) 555-1234",
"425-555-9999",
"406.555.1818",
"+1 206 555 8888",
"4255558872",
]
pattern = /^.*(\d{3}).*(\d{3}).*(\d{4})$/
phone_numbers.each do |number|
match = number.match(pattern)
puts "(#{match[1]}) #{match[2]}-#{match[3]}"
end
# emails = ["[email protected]", "[email protected]", "[email protected]"]
# email_regex = /.*@(.*\..*)/
# # Create a new hash where missing values are initialized to 0
# domain_counts = Hash.new(0)
# emails.each do |email|
# match_result = email.match(email_regex)
# next unless match_result # skip strings that don't match
# domain = match_result[1]
# domain_counts[domain] += 1
# end
# domain_counts.each do |domain, count|
# puts "#{domain}: #{count}"
# end
# pattern = /(.+)@(.+\..+)/
# match_results = "[email protected]".match(pattern)
# puts match_results[0]
# puts match_results[1]
# puts match_results[2]
# text = gets.chomp
# if pattern.match(text)
# puts "it matches"
# else
# puts "that's an invalid phone #"
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment