55分钟学会正则表达式(译)

前端之家收集整理的这篇文章主要介绍了55分钟学会正则表达式(译)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://doslin.com/regular%20expressions/2014/03/11/learn-regular-expressions-in-about-55-minutes.html


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //[self checkNumber:@"a55b2c9"];
    //[self checkChar:@"a55b2c9"];
//    [self checkPassword:@"a55b2c9"];
//    [self checkPassword:@"Q55b2c9"];
//    [self checkPassword:@"5b2cF9"];
    NSLog(@"rs:%@",[self regularPattern:@"5b2Cf9" pattern:@"([a-z]+)&([A-Z]+)&([0-9]+)"]?@"YES":@"NO");
    return YES;
}

//字母、数字
//至少包含一个大写字母、一个小写
//长度为6-8
- (void)checkPassword:(NSString *)networkKey
{
    BOOL result = ([self regularPattern:networkKey pattern:@"[a-zA-Z0-9]{6,8}"]&&
                   [self regularPattern:networkKey pattern:@"[A-Z]{1}"]&&
                   [self regularPattern:networkKey pattern:@"[a-z]{1}"]&&
                   [self regularPattern:networkKey pattern:@"[0-9]{1}"]
                  );
    NSLog(@"rs:%@",result?@"YES":@"NO");
}

- (BOOL) regularPattern:(NSString *)content pattern:(NSString *)pattern
{
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSArray *result = [regex matchesInString:content options:NSMatchingReportProgress range:NSMakeRange(0,[content length])];
    return [result count]>0?YES:NO;
}

//统计两个连续的数字
- (void)check1:(NSString *)str
{
    //[] 只会查找内部的某一个字符而已
    //统计两个连续的数字
    //方法一:
    //NSString *pattern = @"[0-9][0-9]";
    
    //方法二:d:代表数字, \\表示转义
    //NSString *pattern = @"\\d\\dd";
    
    //方法三:d:代表数字, \\表示转义
    NSString *pattern = @"\\d{2}";//@"\\d{2}a{2}":代表连续三个数字两个a
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,[str length])];
    NSLog(@"%zd",result.count);
}

//编计数字
- (void)checkNumber:(NSString *)str
{
    //[] 只会查找内部的某一个字符而已
    //以下两种写法都可,前一个是简写
    //NSString *pattern = @"[0-9]";
    NSString *pattern = @"[0123456789]";
     //1,创建正则表达式
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}

//编计字母
- (void)checkChar:(NSString *)str
{
    //[] 只会查找内部的某一个字符而已
    NSString *pattern = @"[a-zA-Z]";
    //1,result.count);
}

//数字的个数必须是2-4
- (void)check2:(NSString *)str
{
    NSString *pattern = @"\\d{2,4}";//@"34561" :只会返回1 因为正则表达的匹配比较贪婪,按最长的算
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}

// ?  表示0个或1个
// +  表示至少一个
// *  表示0个或多个
// ^  以什么开头,如  @"^\\d"  表示以数字开头
// $  以什么结尾  如 @"\\d$"  表示以数字结尾
// .  表示任意东西
- (void)check3:(NSString *)str
{
    NSString *pattern = @"\\d?";
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}

//以数字开头
- (void)check4:(NSString *)str
{
    NSString *pattern = @"^\\d{3}";//必须以3个数字开头
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}

//以数字开头和结尾
- (void)check5:(NSString *)str
{
    NSString *pattern = @"^\\d[a-zA-Z0-9]*\\d$";//必须以数字开头和结尾,中间可以是0个或多个字符
    //NSString *pattern = @"^\\d.\\d$";//必须以数字开头和结尾,中间只能是1个任意的字符
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}

//验证QQ:
//不能以0开头
//全部是数字
//5-11个数字
- (void)check6:(NSString *)str
{
    NSString *pattern = @"^[1-9]\\d{4,10}$";
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}

//验证手机:
//全部是数字
//11个数字
//以 13,15,17,18开头
- (void)check7:(NSString *)str
{
    NSString *pattern = @"^[1][3578]\\d{9}$";//11-2=9
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}

//判断ip
//中间三个.
//1-3个数字
- (void)check8:(NSString *)str
{
    //以什么开头,什么结尾 : @"^$"
    //@"^\\d{1,3}\\.\\d{1,3}$"   特殊字符必须转义,如:.,需要表示成:\\.
    NSString *pattern = @"^$";//
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}
//JavaScript 的正则表达式:以 /中间的正则/ ,如果需要修改成oc,只需要将去掉首尾即可

//以字母开头
- (void)check9:(NSString *)str
{
    NSString *pattern = @"^[A-Z|a-z]{1}[a-zA-Z0-9]*";
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,[str length])];
    NSLog(@"%@ %zd",str,result.count);
}

//判断以中文中文开头:\\u4e00-\\u9fa5
- (void)check10:(NSString *)str
{
    NSString *pattern = @"[\\u4e00-\\u9fa5]";
    NSRegularExpression *regex = [[NSRegularExpression alloc]initWithPattern:pattern options:nil error:nil];
    NSArray *result = [regex matchesInString:str options:nil range:NSMakeRange(0,result.count);
}

猜你在找的正则表达式相关文章