美文网首页
[Swift4.2] iOS常用正则表达式 enum封装使用

[Swift4.2] iOS常用正则表达式 enum封装使用

作者: 亡鱼 | 来源:发表于2019-03-20 10:17 被阅读0次
    import Foundation
    
    enum RegexValidate {
        
        case email(_: String)       // 邮箱
        case cnPhoneNum(_: String)    // 手机
        case carNum(_: String)      // 车牌号
        case username(_: String)    // 登录名
        case password(_: String)    // 密码
        case nickname(_: String)    // 昵称
        case URL(_: String)         // 网址
        case IP(_: String)          // ip
        case idCard(_: String)      // 身份证号
        case numbers(_: String)     // 只包含数字
        case lettersAndNumbers(_: String) // 只包含字母和数字
        case chinese(_: String)     // 中文
        
        var isRight: Bool {
            var predicateStr: String!
            var currObject:String!
            switch self {
            case let .email(str):
                predicateStr = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
                currObject = str
            case let .cnPhoneNum(str):
                predicateStr = "^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$"
                currObject = str
            case let .carNum(str):
                predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
                currObject = str
            case let .username(str):
                predicateStr = "^[A-Za-z0-9]{6,20}+$"
                currObject = str
            case let .password(str):
                predicateStr = "^[a-zA-Z0-9]{6,24}+$"
                currObject = str
            case let .nickname(str):
                predicateStr = "^[\\u4e00-\\u9fa5]{4,8}$"
                currObject = str
            case let .URL(str):
                predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
                currObject = str
            case let .IP(str):
                predicateStr = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
                currObject = str
            case let .idCard(str):
                predicateStr = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"
                currObject = str
            case let .numbers(str):
                predicateStr = "^[0-9]+$"
                currObject = str
            case let .lettersAndNumbers(str):
                predicateStr = "^[a-zA-Z0-9]{1,30}+$"
                currObject = str
            case let .chinese(str):
                predicateStr = "^[\\u4e00-\\u9fa5]{0,100}$"
                currObject = str
            }
            let predicate = NSPredicate(format: "SELF MATCHES %@" ,predicateStr)
            return predicate.evaluate(with: currObject.trimmingCharacters(in: .whitespacesAndNewlines))
        }
    }
    
        // used  使用方式
        /*
         let vaString: Bool = RegexValidate.chinese("中 ").isRight
         print(vaString)
         // print "ture"
         */
    

    相关文章

      网友评论

          本文标题:[Swift4.2] iOS常用正则表达式 enum封装使用

          本文链接:https://www.haomeiwen.com/subject/ibfgettx.html