Swift - 正则表达式


import UIKit

class MMTest: NSObject {

//MARK:- 正则匹配手机号

class func checkMobile(_ mobileNumbel:NSString) ->Bool

{

   /**

    * 手机号码

    * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188

    * 联通:130,131,132,152,155,156,185,186

    * 电信:133,1349,153,180,189,181(增加)

    */

   let MOBIL = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";

   /**

    * 中国移动:China Mobile

    * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188

    */

   let CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";

   /**

    * 中国联通:China Unicom

    * 130,131,132,152,155,156,185,186

    */

   let CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$";

   /**

    * 中国电信:China Telecom

    * 133,1349,153,180,189,181(增加)

    */

   let CT = "^1((33|53|8[019])[0-9]|349)\\d{7}$";

   let regextestmobile = NSPredicate(format: "SELF MATCHES %@", MOBIL)

   let regextestcm = NSPredicate(format: "SELF MATCHES %@", CM)

   let regextestcu = NSPredicate(format: "SELF MATCHES %@", CU)

   let regextestct = NSPredicate(format: "SELF MATCHES %@", CT)

   if regextestmobile.evaluate(with: mobileNumbel)||regextestcm.evaluate(with: mobileNumbel)||regextestcu.evaluate(with: mobileNumbel)||regextestct.evaluate(with: mobileNumbel) {

       return true

   }

       return false

}

//MARK:- 正则匹配用户身份证号15或18位

class func checkUserIdCard(_ idCard:NSString) ->Bool {

   let pattern = "(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";

   let pred = NSPredicate(format: "SELF MATCHES %@", pattern)

   let isMatch:Bool = pred.evaluate(with: idCard)

   return isMatch;

}

//MARK:- 正则匹配用户密码6-18位数字和字母组合

class func checkPassword(_ password:NSString) ->Bool {

   let pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}"

   let pred = NSPredicate(format: "SELF MATCHES %@", pattern)

   let isMatch:Bool = pred.evaluate(with: password)

   return isMatch;

}

//MARK:- 正则匹配URL

class func checkURL(_ url:NSString) ->Bool {

   let pattern = "^[0-9A-Za-z]{1,50}"

   let pred = NSPredicate(format: "SELF MATCHES %@", pattern)

   let isMatch:Bool = pred.evaluate(with: url)

   return isMatch;

}

//MARK:- 正则匹配用户姓名,20位的中文或英文

class func checkUserName(_ userName:NSString) ->Bool {

   let pattern = "^[a-zA-Z\\u4E00-\\u9FA5]{1,20}"

   let pred = NSPredicate(format: "SELF MATCHES %@", pattern)

   let isMatch:Bool = pred.evaluate(with: userName)

   return isMatch;

}

//MARK:- 正则匹配用户email

class func checkEmail(_ email:NSString) ->Bool {

   let pattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"

   let pred = NSPredicate(format: "SELF MATCHES %@", pattern)

   let isMatch:Bool = pred.evaluate(with: email)

   return isMatch;

}

}

import Foundation

//正则表达式

struct RegexHelper {

let regex:NSRegularExpression?

init(_ pattern:String) {

regex = try! NSRegularExpression(pattern: pattern, options: .CaseInsensitive)

}

func match(input:String) -> Bool {

if let matches = regex?.matchesInString(input, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, input.characters.count)){

return matches.count > 0

}else{

return false

}

}

}

//声明操作符 infix operator =~ {

associativity none

precedence 130

}

func =~(lhs:String,rhs:String) -> Bool { return RegexHelper(rhs).match(lhs) }

//检测是否是邮箱

func isMail() -> Bool {

if "zhaiting0325@qq.com" =~ "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$" {

return true

}else{

return false

}

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容