此篇无图
App开发中少不了正则表达式的使用,一个统一的正则表达式扩展组件是有效减少代码量,规范项目代码,利我利他的事情。常用的正则有:密码、邮箱、手机号、名称,度娘告诉我们的写法多种多样却不一定有效,(如:很多手机号的写法还用上古时期的 ^13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$ 这简直就是误人子弟的事情)
包含对 NSRegularExpression 的扩展 和常用表达式的集合 enum CD_RegEx{},让使用正则表达式更便捷。当然,CD_RegEx提供的集合不一定满足需求,可以仿照 CD_RegEx 的写法,或其他写法来布局自己系统的表达式。
干说不练假把式!贴代码:
- NSRegularExpression 示例
/// NSRegularExpression 初始化NSRegularExpression.cd_init(pattern)orCD.makeRegEx(pattern)复制代码
//MARK:--- RegEx ----------do{//验证一个邮箱地址的格式是否正确。 let pattern = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$" let email = "dsfddf@qq.163.com" if (CD.makeRegEx(pattern)?.cd.match(allIn: email)) != nil { _ = true }else{ _ = false } if CD.makeRegEx(pattern)!.cd.match(email) { _ = true }else{ _ = false }}do{// 获取第一个匹配结果 let pattern = "([\\u4e00-\\u9fa5]+):([\\d]+)" let str = "张三:123456 66,99 ,我的, 李四:23457,王麻子:110987 9" if let text = CD.makeRegEx(pattern)?.cd.match(firstIn: str) { //print(text.cd.range(in: str)) text.cd.string(in: str) text.cd.captures(in: str) text.cd.captures(in: str)![0] text.cd.captures(in: str)![1] }else{ _ = false }}do{// 获取所有匹配结果 let pattern = "([\\u4e00-\\u9fa5]+):([\\d]+)" let str = "张三:123456 66,99 ,我的, 李四:23457,王麻子:110987 9" if let text = CD.makeRegEx(pattern)?.cd.match(allIn: str) { text.map{ $0.cd.string(in: str)} text.map{ $0.cd.captures(in: str)![0]} text.map{ $0.cd.captures(in: str)![1]} }else{ _ = false }}do{// ----- 字符串替换 let pattern = "([\\u4e00-\\u9fa5]+):([\\d]+)" let str = "张三:123456 66,99 ,我的, 李四:23457,王麻子:110987 9" CD.makeRegEx(pattern)!.cd.match(replaceIn: str, with: "***", count: 1) CD.makeRegEx(pattern)!.cd.match(replaceIn: str, with: "***") CD.makeRegEx(pattern)!.cd.match(replaceIn: str, with: "***", count: 9)}do{//捕获组替换 let pattern = "([\\u4e00-\\u9fa5]+):([\\d]+)" let str = "张三:123456 66,99 ,我的, 李四:23457,王麻子:110987 9" CD.makeRegEx(pattern)!.cd.match(replaceIn: str, with: "$1的编号是$2", count: 1) CD.makeRegEx(pattern)!.cd.match(replaceIn: str, with: "$1的编号是$2") CD.makeRegEx(pattern)!.cd.match(replaceIn: str, with: "$1的编号是$2", count: 9)}复制代码
- CD_RegEx 示例
do{ CD_RegEx.match("123", type: CD_RegEx.tPassword(CD_RegEx.Password.value0)) CD_RegEx.match("123456", type: CD_RegEx.tPassword(CD_RegEx.Password.value0)) CD_RegEx.match("a123", type: CD_RegEx.tPassword(CD_RegEx.Password.value1)) CD_RegEx.match("a123456", type: CD_RegEx.tPassword(CD_RegEx.Password.value1)) CD_RegEx.match("123", type: CD_RegEx.tPassword(CD_RegEx.Password.value2)) CD_RegEx.match("123_456", type: CD_RegEx.tPassword(CD_RegEx.Password.value2)) CD_RegEx.match("123_a456", type: CD_RegEx.tPassword(CD_RegEx.Password.value2)) CD_RegEx.match("123_a@456", type: CD_RegEx.tPassword(CD_RegEx.Password.value2)) // 必须包含大小写字母和数字 CD_RegEx.match("1234567a", type: CD_RegEx.tPassword(CD_RegEx.Password.value3("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$"))) CD_RegEx.match("1234567aA_@", type: CD_RegEx.tPassword(CD_RegEx.Password.value3("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$"))) CD_RegEx.match("1382828", type: CD_RegEx.tMobile(nil)) CD_RegEx.match("13828282626", type: CD_RegEx.tMobile(nil)) CD_RegEx.match("https://itunes.apple.com/cn/app/", type: CD_RegEx.tURL(nil)) CD_RegEx.match("htt://itunes.apple.com/cn/app/", type: CD_RegEx.tURL(nil))}复制代码