0

swift 正则表达式匹配替换字符串

miriam created at6 years ago view count: 3136

swift 怎样正则表达式匹配替换字符串?

比如把[email protected] 替换为 root#baidu.com

report
回复
0
let txt = "[email protected]"
let regex = NSRegularExpression(pattern: "([^@\\s]+)@test\\.(\\w+)", options:nil, error: nil)
let newString = regex!.stringByReplacingMatchesInString(txt, options: nil, range: NSMakeRange(0, count(txt)), withTemplate: "$1@baidu.$2")
println(newString)
6 years ago 回复