卡号屏蔽
Card Number Masking
我正在尝试使用正则表达式屏蔽卡号。
可以显示前 6 个和最后 2 个字符,其他应该是“*”。
let cardNumber: String = "5890040000000016"
print("Output: ", cardNumber.mask(regexPattern:"XXXXXXX")) // Output: 589004********16
我如何在 swift 中做到这一点?
您可以将子字符串连接在一起:
let cardNumber = "5890040000000016"
let start = cardNumber.startIndex ..< cardNumber.index(cardNumber.startIndex, offsetBy: 6)
let end = cardNumber.index(cardNumber.endIndex, offsetBy: -2) ..< cardNumber.endIndex
let result = cardNumber[start] + Array(repeating: "*", count: cardNumber.count - 8) + cardNumber[end]
print(result)
// Prints: 589004********16
也许您正在寻找这样的东西:
let s = "5890040000000016"
let patt = ##"^(\d{6})(.*)(\d{2})$"## // this is your pattern
let reg = try! NSRegularExpression(pattern: patt, options: [])
if let m = reg.firstMatch(in: s, options: [], range: NSRange(s.startIndex..<s.endIndex, in: s)) {
let r = m.range(at:2)
let result = s.replacingCharacters(in: Range(r, in:s)!, with: String(repeating: "X", count: r.length))
print(result) // 589004XXXXXXXX16
}
您的问题包含两个重要部分:
匹配所有字符但前 6 个和最后 2 个字符的正则表达式
let regexPattern = "(?<=.{6}).(?=.*.{2}$)"
和
String
上的扩展,采用正则表达式并对其进行屏蔽:
extension String {
func masked(matching regexPattern: String, with template: String = "*") throws -> String {
let regex = try NSRegularExpression(pattern: regexPattern, options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, count)
return regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: template)
}
}
用法:
let cardNumber = "5890040000000016"
let regexPattern = "(?<=.{6}).(?=.*.{2}$)"
print("Output:", try! cardNumber.masked(matching: regexPattern))
Output: 589004********16
我正在尝试使用正则表达式屏蔽卡号。 可以显示前 6 个和最后 2 个字符,其他应该是“*”。
let cardNumber: String = "5890040000000016"
print("Output: ", cardNumber.mask(regexPattern:"XXXXXXX")) // Output: 589004********16
我如何在 swift 中做到这一点?
您可以将子字符串连接在一起:
let cardNumber = "5890040000000016"
let start = cardNumber.startIndex ..< cardNumber.index(cardNumber.startIndex, offsetBy: 6)
let end = cardNumber.index(cardNumber.endIndex, offsetBy: -2) ..< cardNumber.endIndex
let result = cardNumber[start] + Array(repeating: "*", count: cardNumber.count - 8) + cardNumber[end]
print(result)
// Prints: 589004********16
也许您正在寻找这样的东西:
let s = "5890040000000016"
let patt = ##"^(\d{6})(.*)(\d{2})$"## // this is your pattern
let reg = try! NSRegularExpression(pattern: patt, options: [])
if let m = reg.firstMatch(in: s, options: [], range: NSRange(s.startIndex..<s.endIndex, in: s)) {
let r = m.range(at:2)
let result = s.replacingCharacters(in: Range(r, in:s)!, with: String(repeating: "X", count: r.length))
print(result) // 589004XXXXXXXX16
}
您的问题包含两个重要部分:
匹配所有字符但前 6 个和最后 2 个字符的正则表达式
let regexPattern = "(?<=.{6}).(?=.*.{2}$)"
和
String
上的扩展,采用正则表达式并对其进行屏蔽:
extension String {
func masked(matching regexPattern: String, with template: String = "*") throws -> String {
let regex = try NSRegularExpression(pattern: regexPattern, options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, count)
return regex.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: template)
}
}
用法:
let cardNumber = "5890040000000016"
let regexPattern = "(?<=.{6}).(?=.*.{2}$)"
print("Output:", try! cardNumber.masked(matching: regexPattern))
Output: 589004********16