Skip to content

Instantly share code, notes, and snippets.

@Stephenitis
Last active December 15, 2015 10:08
Show Gist options
  • Save Stephenitis/5243065 to your computer and use it in GitHub Desktop.
Save Stephenitis/5243065 to your computer and use it in GitHub Desktop.
regex on socrates exercise
# Determine whether a string contains a Social Security number.
def has_ssn?(string)
if string.scan(/\d\d\d.\d\d.\d\d\d\d/)[0] == nil
false
elsif string.scan(/\d\d\d.\d\d.\d\d\d\d/)[0] != nil
true
end
end
# Return the Social Security number from a string.
def grab_ssn(string)
string.scan(/\d\d\d.\d\d.\d\d\d\d/)[0]
end
# Return all of the Social Security numbers from a string.
def grab_all_ssns(string)
string.scan(/\d\d\d.\d\d.\d\d\d\d/)
end
# Obfuscate all of the Social Security numbers in a string. Example: XXX-XX-4430.
def hide_all_ssns(string)
string.gsub(/\d\d\d-\d\d/, "XXX-XX")
end
# Ensure all of the Social Security numbers use dashes for delimiters.
# Example: 480.01.4430 and 480014430 would both be 480-01-4430.
def format_ssns(string)
string.match(/\d{3}.\d{2}.\d{4}|\d{9}/) ? string.gsub(/(\d{3})(\d{2})(\d{4})/, "\\1-\\2-\\3").gsub(".", "-") : string
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment