Skip to content

Instantly share code, notes, and snippets.

@kvirani
Last active February 7, 2019 18:30
Show Gist options
  • Save kvirani/88574d47bb14f2aac852 to your computer and use it in GitHub Desktop.
Save kvirani/88574d47bb14f2aac852 to your computer and use it in GitHub Desktop.
Regular Expressions Exercise

Ruby's String class has over 100 methods that give Ruby programmers the ability to process, manipulate, and transform textual data.

About a dozen of those String methods use Regular Expressions in order to allow for high-powered string matching, giving us great power. But remember: With great power comes great responsibility.

An example of low-powered string matching is when you use your browser's search (⌘f on a Mac) for a specific word, say, your own name. What if you wanted to search your browser for anything that matches the pattern for a social insurance number? For that, you'd use Regular Expressions.

As you work through the exercises of this challenge, it's recommended that you use http://rubular.com to help you code your regex. You can refer to / read the the following if you get stuck:

  1. Pickaxe Guide
  2. Using Regular Expressions in Ruby

Objectives

  1. Use the driver code to guide you to a working solution.
  2. Add additional driver code where you want to feel more comfortable with your implementation. For instance, what happens when you pass a string with only a single SIN into the hide_all_sins or get_all_sins methods?
  3. Did adding driver code help you solve the challenge more effectively? How? (Answer in the gist as a comment)
# Determine whether a string contains a SIN (Social Insurance Number).
# A SIN is 9 digits and we are assuming that they must have dashes in them
def has_sin?(string)
end
puts "has_sin? returns true if it has what looks like a SIN"
puts has_sin?("please don't share this: 234-604-142") == true
puts "has_sin? returns false if it doesn't have a SIN"
puts has_sin?("please confirm your identity: XXX-XXX-142") == false
puts has_sin?("please don't share this: 234-6043-142") == false
puts has_sin?("please don't share this: 2342-604-142") == false
puts has_sin?("please don't share this: 234-604-1421") == false
# Return the Social Insurance Number from a string.
def grab_sin(string)
end
puts "grab_sin returns an SIN if the string has an SIN"
puts grab_sin("please don't share this: 234-604-142") == "234-604-142"
puts "grab_sin returns nil if it doesn't have a SIN"
puts grab_sin("please confirm your identity: XXX-XXX-142") == nil
# Return all of the SINs from a string, not just one.
def grab_all_sins(string)
end
puts "grab_all_sins returns all SINs if the string has any SINs"
puts grab_all_sins("234-604-142, 350-802-074, 013-630-876") == ["234-604-142", "350-802-074", "013-630-876"]
puts "grab_all_sins returns an empty Array if it doesn't have any SINs"
puts grab_all_sins("please confirm your identity: XXX-XXX-142") == []
# Obfuscate all of the Social Insurance numbers in a string. Example: XXX-XX-4430.
def hide_all_sins(string)
end
puts "hide_all_sins obfuscates any SINs in the string"
puts hide_all_sins("234-601-142, 350-801-074, 013-601-876") == "XXX-XXX-142, XXX-XXX-074, XXX-XXX-876"
puts "hide_all_sins does not alter a string without SINs in it"
string = "please confirm your identity: XXX-XXX-142"
puts hide_all_sins(string) == string
# Ensure all of the Social Insurance numbers use dashes for delimiters.
# Example: 480.01.4430 and 480014430 would both be 480-01-4430.
def format_sins(string)
end
puts "format_sins finds and reformat any SINs in the string"
puts format_sins("234600142, 350.800.074, 013-600-876") == "234-600-142, 350-800-074, 013-600-876"
puts "format_sins does not alter a string without SINs in it"
string = "please confirm your identity: 4421422"
puts format_sins(string) == string
string = "please confirm your identity: 123abc445"
puts format_sins(string) == string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment