Last active
December 21, 2015 22:09
-
-
Save MrBean83/6373407 to your computer and use it in GitHub Desktop.
"Regular Expressions"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Determine whether a string contains a Social Security number. | |
| def has_ssn?(string) | |
| /\d{3}-\d{2}-\d{4}/.match(string) | |
| end | |
| # Return the Social Security number from a string. | |
| def grab_ssn(string) | |
| def grab_ssn(string) | |
| if /\d{3}-\d{2}-\d{4}/.match(string) | |
| string.match(/\d{3}-\d{2}-\d{4}/).to_s | |
| end | |
| end | |
| # Return all of the Social Security numbers from a string. | |
| def grab_all_ssns(string) | |
| unless /\d{3}-\d{2}-\d{4}/.match(string) | |
| return Array.new | |
| else | |
| string.scan(/\d{3}-\d{2}-\d{4}/) | |
| end | |
| end | |
| # Obfuscate all of the Social Security numbers in a string. Example: XXX-XX-4430. | |
| def hide_all_ssns(string) | |
| if /\d{3}-\d{2}-\d{4}/.match(string) | |
| string.gsub(/\d{3}-\d{2}-/,"XXX-XX-") #scan(/X{3}-X{2}-\d{4}/) | |
| #string.scan(/\d{3}-\d{2}-\d{4}/) | |
| else | |
| return string | |
| end | |
| 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) | |
| unless /\d{3}-\d{2}-\d{4}/.match(string) | |
| return string | |
| else | |
| string.gsub(/\.+/, "-").gsub(/(\d{3})(\d{2})(\d{3})/, '\1-\2-\3') | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment