正则表达式Swift示例

正则表达式

让字符串只保留数字、字母和汉字

let info = "2\n34`2\t3!@#$%^&*()张5*2三"
//^的含义是取反
let r = try! NSRegularExpression(pattern: "[^a-zA-Z0-9\u{4e00}-\u{9fa5}]", options: .allowCommentsAndWhitespace)
let newInfo = r.stringByReplacingMatches(in: info, options: .reportProgress, range: NSRange(location: 0, length: info.count), withTemplate: "")

print(info)
print("-----------------------")
print(newInfo)

过滤html标签和换行符

//正则表达式为
<[^>]*>|\\n

// html的正则为
<[^>]*>

// 换行的正则为
\\n

// | 代表拼接规则的含义

正则的规则图:
正则表达式Swift示例_第1张图片

你可能感兴趣的:(IOS开发,正则表达式,swift,开发语言)