Skip to content

Instantly share code, notes, and snippets.

@BrianJoyce
Created October 10, 2012 07:56
Show Gist options
  • Save BrianJoyce/3863907 to your computer and use it in GitHub Desktop.
Save BrianJoyce/3863907 to your computer and use it in GitHub Desktop.
regex
require 'pry'
# Determine whether a string contains a Social Security number.
def has_ssn?(string)
string.match(/\d{3}.\d{2}.\d{4}/) ? true : false
end
# Return the Social Security number from a string.
def grab_ssn(string)
string.slice(/\d{3}.\d{2}.\d{4}/) # ? true : false
end
# Return all of the Social Security numbers from a string.
def grab_all_ssns(string)
string.scan(/\d{3}.\d{2}.\d{4}/)
end
# Obfuscate all of the Social Security numbers in a string. Example: XXX-XX-4430.
def hide_all_ssns(string)
string.gsub!(/\d{3}.\d{2}/,"XXX-XX").scan(/X{3}.X{2}.\d{4}/)
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.scan(/\d{3}.\d{2}.\d{4}/).
end
[\d{3}\d{2}\d{4}]
puts has_ssn?("please don't share this: 234-60-1422")
puts grab_ssn("please don't share this: 234-60-1422")
puts grab_all_ssns("234-60-1422, 350-80-0744, 013-60-8762")
puts hide_all_ssns("234-60-1422, 350-80-0744, 013-60-8762")
binding.pry
"234-60-1422, 350-80-0744, 013-60-8762".scan(/\d{3}.\d{2}.\d{4}/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment