替换子字符串
模式可用于替换输入字符串的一部分。
下面的示例用 $
替换分号。
var money = "¢¥€£$¥€£¢"
let pattern = "¢"
do {
let regEx = try NSRegularExpression (pattern: pattern, options: [])
let nsString = money as NSString
let range = NSMakeRange(0, nsString.length)
let correct$ = regEx.stringByReplacingMatches(in: money, options: .withTransparentBounds, range: range, withTemplate: "$")
} catch let error as NSError {
print("Matching failed")
}
//correct$ = "$¥€£$¥€£$"