正则表达式-03 更多的方式

前端之家收集整理的这篇文章主要介绍了正则表达式-03 更多的方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这篇文章主要是学习剩下的一些常见的规则语法

博文代码(点击下载)

^: 非运算 a[^b] 除了b以为的字符 或者^a 以a开头 
\d:代表一个数字,等同于[0-9] 
\D:代表⾮数字,等同于[^0-9]
\s:代表换行符、Tab制表符等空⽩字符
\S:代表⾮空白字符 
\w:匹配任何字类字符,包括下划线。与“[A-Za-z0-9_]”等效。 
\W:⾮\w,等同于[^\w]

^的语法代码:

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk.com";

        NSString* pattern = @"h[^ttp]";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0,string.length)];

        if(resultArray.count){
            for (NSTextCheckingResult* result in resultArray) {
                NSLog(@"%@",[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"检索无结果");

        }
    }
 return 0;
}

运行结果:h[^ttp]————不以http开头的字符

RegularExpression03[3261:303] ho

代码:

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk.com";

        NSString* pattern = @"^http";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0,[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"检索无结果");

        }
    }
 return 0;
}

运行结果:没有中括号括着,就是以^后面字符串开头.所以^http,就是要以http开头的字符

RegularExpression03[3276:303] http

\d的语法代码:代表一个数字,等同于[0-9]

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk123.com";

        NSString* pattern = @"\\d";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0,[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"检索无结果");

        }
    }
 return 0;
}

运行结果:

RegularExpression03[3288:303] 1
RegularExpression03[3288:303] 2
RegularExpression03[3288:303] 3

\D的语法代码:代表⾮数字,等同于[^0-9]

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk123.com";

        NSString* pattern = @"\\D";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0,[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"检索无结果");

        }
    }
 return 0;
}

运行结果:

RegularExpression03[3297:303] h
RegularExpression03[3297:303] t
RegularExpression03[3297:303] t
RegularExpression03[3297:303] p
RegularExpression03[3297:303] : RegularExpression03[3297:303] / RegularExpression03[3297:303] / RegularExpression03[3297:303] w RegularExpression03[3297:303] w RegularExpression03[3297:303] w RegularExpression03[3297:303] . RegularExpression03[3297:303] h RegularExpression03[3297:303] o RegularExpression03[3297:303] u RegularExpression03[3297:303] s RegularExpression03[3297:303] n RegularExpression03[3297:303] k RegularExpression03[3297:303] . RegularExpression03[3297:303] c RegularExpression03[3297:303] o RegularExpression03[3297:303] m

\s的语法代码:代表换行符、Tab制表符等空⽩字符

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk 123.com";

        NSString* pattern = @"\\s";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0,[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"检索无结果");

        }
    }
 return 0;
}

运行结果:

RegularExpression03[3317:303]

\S的语法代码:代表⾮空白字符

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk 123.com";

        NSString* pattern = @"\\S";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0,[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"检索无结果");

        }
    }
 return 0;
}

运行结果:

RegularExpression03[3297:303] h
RegularExpression03[3297:303] t
RegularExpression03[3297:303] t
RegularExpression03[3297:303] p
RegularExpression03[3297:303] : RegularExpression03[3297:303] / RegularExpression03[3297:303] / RegularExpression03[3297:303] w RegularExpression03[3297:303] w RegularExpression03[3297:303] w RegularExpression03[3297:303] . RegularExpression03[3297:303] h RegularExpression03[3297:303] o RegularExpression03[3297:303] u RegularExpression03[3297:303] s RegularExpression03[3297:303] n RegularExpression03[3297:303] k RegularExpression03[3297:303] 1 RegularExpression03[3297:303] 2 RegularExpression03[3297:303] 3 RegularExpression03[3297:303] . RegularExpression03[3297:303] c RegularExpression03[3297:303] o RegularExpression03[3297:303] m

\w的语法代码:匹配任何字类字符,包括下划线。与“[A-Za-z0-9_]”等效。

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk 123.com";

        NSString* pattern = @"\\w";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0,[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"检索无结果");

        }
    }
 return 0;
}

运行效果:

RegularExpression03[3297:303] h
RegularExpression03[3297:303] t
RegularExpression03[3297:303] t
RegularExpression03[3297:303] p
RegularExpression03[3297:303] : RegularExpression03[3297:303] / RegularExpression03[3297:303] / RegularExpression03[3297:303] w RegularExpression03[3297:303] w RegularExpression03[3297:303] w RegularExpression03[3297:303] . RegularExpression03[3297:303] h RegularExpression03[3297:303] o RegularExpression03[3297:303] u RegularExpression03[3297:303] s RegularExpression03[3297:303] n RegularExpression03[3297:303] k RegularExpression03[3297:303] 1 RegularExpression03[3297:303] 2 RegularExpression03[3297:303] 3 RegularExpression03[3297:303] . RegularExpression03[3297:303] c RegularExpression03[3297:303] o RegularExpression03[3297:303] m

\W的语法代码:⾮\w,等同于[^\w]

int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSString* string  = @"http://www.housnk 123.com";

        NSString* pattern = @"\\W";

        NSError* error = [[NSError alloc]init];

        NSRegularExpression* regularExpression  = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];

        NSArray* resultArray = [regularExpression matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0,[string substringWithRange:result.range]);
            }
        }else{
            NSLog(@"检索无结果");

        }
    }
 return 0;
}

运行结果:

RegularExpression03[3342:303] : RegularExpression03[3342:303] / RegularExpression03[3342:303] / RegularExpression03[3342:303] . RegularExpression03[3342:303] RegularExpression03[3342:303] .

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