-
-
Save cardoso/a0778e023bcb5b1f7e30bce14d490791 to your computer and use it in GitHub Desktop.
This file contains 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
func redactCreditCardNumbers(from text: String) -> String { | |
var text = text | |
let americanExpress = "(?:3[47][0-9]{13})"; | |
let dinersClub = "(?:3(?:0[0-5]|[68][0-9])[0-9]{11})"; | |
let discover = "(?:6(?:011|5[0-9]{2})(?:[0-9]{12}))"; | |
let jcb = "(?:(?:2131|1800|35\\d{3})\\d{11})"; | |
let maestro = "(?:(?:5[0678]\\d\\d|6304|6390|67\\d\\d)\\d{8,15})"; | |
let mastercard = "(?:(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12})"; | |
let visa = "(?:4[0-9]{12})(?:[0-9]{3})?"; | |
let all = [americanExpress, dinersClub, discover, jcb, maestro, mastercard, visa].joined(separator: "|") | |
let regex = try! NSRegularExpression(pattern: all) | |
let res = regex.matches(in: text, options: [], range: NSRange(location: 0, length: text.count)) | |
res.forEach { | |
text = text.replacingCharacters( | |
in: Range($0.range, in: text)!, | |
with: String(repeating: "*", count: $0.range.length) | |
) | |
} | |
return text | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment