正则表达式(实用篇)

作者: SuyChen | 来源:发表于2016-04-29 15:09 被阅读430次

    大家好,本人总结了一些实用的正则表达式判断方法,经测试可以用(仅针对iOS),如果还有其他常用的方法,大家可以写出来互相学习,谢谢思密达!

    //匹配Email

    - (BOOL)isEmail:(NSString*)str
    {
        NSString *regex = @"^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+(com|net|org|edu|gov|cn|info)$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    //匹配正数

    - (BOOL)isNumber:(NSString*)str
    {
        NSString *regex = @"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    //匹配正整数

    - (BOOL)isNumber1:(NSString*)str
    {
        NSString *regex = @"^[1-9]\\d*$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    //匹配正整数或0

    - (BOOL)isNumber2:(NSString*)str
    {
        NSString *regex = @"^[0-9]\\d*$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    //匹配整数

    - (BOOL)isNumber3:(NSString*)str
    {
        NSString *regex = @"^-?[1-9]\\d*$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    //匹配非负数

    - (BOOL)isNumber4:(NSString*)str
    {
        NSString *regex = @"^\\d+(\\.\\d+)?$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    //匹配手机

    - (BOOL)isMobile:(NSString*)str
    {
        NSString *regex = @"^(13[0-9]|15[0-9]|18[0-9]|14[0-9]]|17[0-9])\\d{8}$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    //匹配英文数字

    - (BOOL)isEnglishAndNumber:(NSString*)str
    {
        NSString *regex = @"^[A-Za-z0-9]+$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    //匹配中文

    - (BOOL)checkName:(NSString*)str{
        NSString *regex = @"^[\u4E00-\u9FA5]+$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    // 校验密码强度

    //密码的强度必须是包含大小写字母和数字的组合,不能使用特殊字符,长度在8-10之间。
    - (BOOL)checkPassword:(NSString*)str{
        NSString *regex = @"^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pre evaluateWithObject:str];
    }
    

    相关文章

      网友评论

      本文标题:正则表达式(实用篇)

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