美文网首页
正则表达式

正则表达式

作者: itclimb | 来源:发表于2017-08-10 21:00 被阅读11次
    正则表达式可以用于指定格式的判断
    • 手机号的判断
    • 身份证号的判断
    1. 手机号码的判断
    static func isValidPhoneNumber(_ phoneNumber:String)->Bool{
            
            let mobile = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"       //基本格式
            let  CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"//移动
            let  CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$"                //联通
            let  CT = "^1((33|53|73|8[09])[0-9]|349)\\d{7}$"          //电信
            
            let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
            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: phoneNumber) == true)
                || (regextestcm.evaluate(with: phoneNumber)  == true)
                || (regextestct.evaluate(with: phoneNumber) == true)
                || (regextestcu.evaluate(with: phoneNumber) == true)){
                return true
            }else {
                return false
            }
        }
    
    
    1. 身份证号判断
        /// 判断是否为合法的身份证号
        /// - Parameter sfz: 身份证
        /// - Returns: 是否合法
        static func isValidateIDCardNumber(sfz:String)->(Bool){
            let value = sfz.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
            var length = 0
            if value == ""{
                return false
            }else{
                length = value.characters.count
                if length != 15 && length != 18{
                    return false
                }
            }
            
            //省份代码
            let arearsArray = ["11","12", "13", "14",  "15", "21",  "22", "23",  "31", "32",  "33", "34",  "35", "36",  "37", "41",  "42", "43",  "44", "45",  "46", "50",  "51", "52",  "53", "54",  "61", "62",  "63", "64",  "65", "71",  "81", "82",  "91"]
            let valueStart2 = (value as NSString).substring(to: 2)
            var arareFlag = false
            if arearsArray.contains(valueStart2){
                arareFlag = true
            }
            if !arareFlag{
                return false
            }
            var regularExpression = NSRegularExpression()
            
            var numberofMatch = Int()
            var year = 0
            switch (length){
            case 15:
                year = Int((value as NSString).substring(with: NSRange(location:6,length:2)))!
                if year%4 == 0 || (year%100 == 0 && year%4 == 0){
                    do{
                        regularExpression = try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$", options: .caseInsensitive) //检测出生日期的合法性
                    }catch{}                
                }else{
                    do{
                        regularExpression =  try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$", options: .caseInsensitive) //检测出生日期的合法性
                    }catch{}
                }
                
                numberofMatch = regularExpression.numberOfMatches(in: value, options:NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, value.characters.count))
                
                if(numberofMatch > 0) {
                    return true
                }else {
                    return false
                }
                
            case 18:
                year = Int((value as NSString).substring(with: NSRange(location:6,length:4)))!
                if year%4 == 0 || (year%100 == 0 && year%4 == 0){
                    do{
                        regularExpression = try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$", options: .caseInsensitive) //检测出生日期的合法性
                    }catch{}
                }else{
                    do{
                        regularExpression =  try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$", options: .caseInsensitive) //检测出生日期的合法性
                    }catch{}
                }
                
                numberofMatch = regularExpression.numberOfMatches(in: value, options:NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, value.characters.count))
                
                if(numberofMatch > 0) {
                    let s =
                        (Int((value as NSString).substring(with: NSRange(location:0,length:1)))! +
                            Int((value as NSString).substring(with: NSRange(location:10,length:1)))!) * 7 +
                            (Int((value as NSString).substring(with: NSRange(location:1,length:1)))! +
                                Int((value as NSString).substring(with: NSRange(location:11,length:1)))!) * 9 +
                            (Int((value as NSString).substring(with: NSRange(location:2,length:1)))! +
                                Int((value as NSString).substring(with: NSRange(location:12,length:1)))!) * 10 +
                            (Int((value as NSString).substring(with: NSRange(location:3,length:1)))! +
                                Int((value as NSString).substring(with: NSRange(location:13,length:1)))!) * 5 +
                            (Int((value as NSString).substring(with: NSRange(location:4,length:1)))! +
                                Int((value as NSString).substring(with: NSRange(location:14,length:1)))!) * 8 +
                            (Int((value as NSString).substring(with: NSRange(location:5,length:1)))! +
                                Int((value as NSString).substring(with: NSRange(location:15,length:1)))!) * 4 +
                            (Int((value as NSString).substring(with: NSRange(location:6,length:1)))! +
                                Int((value as NSString).substring(with: NSRange(location:16,length:1)))!) *  2 +
                            Int((value as NSString).substring(with: NSRange(location:7,length:1)))! * 1 +
                            Int((value as NSString).substring(with: NSRange(location:8,length:1)))! * 6 +
                            Int((value as NSString).substring(with: NSRange(location:9,length:1)))! * 3
                    
                    let Y = s%11
                    var M = "F"
                    let JYM = "10X98765432"
                    
                    M = (JYM as NSString).substring(with: NSRange(location:Y,length:1))
                    if M == (value as NSString).substring(with: NSRange(location:17,length:1))
                    {
                        return true
                    }else{return false}
    
                }else {
                    return false
                }
                
            default:
                return false
            }
        }
    
    

    这两个是个人信息常用到的判断,其中的正则也可根据实际的业务流程做修改,以上都是swift3.0版本的

    相关文章

      网友评论

          本文标题:正则表达式

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