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

来源:互联网 发布:淘宝博娱乐nb88.com 编辑:程序博客网 时间:2024/05/20 18:51

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

博文代码(点击下载)

^: 非运算 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.length)];        if(resultArray.count){            for (NSTextCheckingResult* result in resultArray) {                NSLog(@"%@",[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.length)];        if(resultArray.count){            for (NSTextCheckingResult* result in resultArray) {                NSLog(@"%@",[string substringWithRange:result.range]);            }        }else{            NSLog(@"检索无结果");        }    }    return 0;}

运行结果:

RegularExpression03[3288:303] 1RegularExpression03[3288:303] 2RegularExpression03[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.length)];        if(resultArray.count){            for (NSTextCheckingResult* result in resultArray) {                NSLog(@"%@",[string substringWithRange:result.range]);            }        }else{            NSLog(@"检索无结果");        }    }    return 0;}

运行结果:

RegularExpression03[3297:303] hRegularExpression03[3297:303] tRegularExpression03[3297:303] tRegularExpression03[3297:303] pRegularExpression03[3297:303] :RegularExpression03[3297:303] /RegularExpression03[3297:303] /RegularExpression03[3297:303] wRegularExpression03[3297:303] wRegularExpression03[3297:303] wRegularExpression03[3297:303] .RegularExpression03[3297:303] hRegularExpression03[3297:303] oRegularExpression03[3297:303] uRegularExpression03[3297:303] sRegularExpression03[3297:303] nRegularExpression03[3297:303] kRegularExpression03[3297:303] .RegularExpression03[3297:303] cRegularExpression03[3297:303] oRegularExpression03[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.length)];        if(resultArray.count){            for (NSTextCheckingResult* result in resultArray) {                NSLog(@"%@",[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.length)];        if(resultArray.count){            for (NSTextCheckingResult* result in resultArray) {                NSLog(@"%@",[string substringWithRange:result.range]);            }        }else{            NSLog(@"检索无结果");        }    }    return 0;}

运行结果:

RegularExpression03[3297:303] hRegularExpression03[3297:303] tRegularExpression03[3297:303] tRegularExpression03[3297:303] pRegularExpression03[3297:303] :RegularExpression03[3297:303] /RegularExpression03[3297:303] /RegularExpression03[3297:303] wRegularExpression03[3297:303] wRegularExpression03[3297:303] wRegularExpression03[3297:303] .RegularExpression03[3297:303] hRegularExpression03[3297:303] oRegularExpression03[3297:303] uRegularExpression03[3297:303] sRegularExpression03[3297:303] nRegularExpression03[3297:303] kRegularExpression03[3297:303] 1RegularExpression03[3297:303] 2RegularExpression03[3297:303] 3RegularExpression03[3297:303] .RegularExpression03[3297:303] cRegularExpression03[3297:303] oRegularExpression03[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.length)];        if(resultArray.count){            for (NSTextCheckingResult* result in resultArray) {                NSLog(@"%@",[string substringWithRange:result.range]);            }        }else{            NSLog(@"检索无结果");        }    }    return 0;}

运行效果:

RegularExpression03[3297:303] hRegularExpression03[3297:303] tRegularExpression03[3297:303] tRegularExpression03[3297:303] pRegularExpression03[3297:303] :RegularExpression03[3297:303] /RegularExpression03[3297:303] /RegularExpression03[3297:303] wRegularExpression03[3297:303] wRegularExpression03[3297:303] wRegularExpression03[3297:303] .RegularExpression03[3297:303] hRegularExpression03[3297:303] oRegularExpression03[3297:303] uRegularExpression03[3297:303] sRegularExpression03[3297:303] nRegularExpression03[3297:303] kRegularExpression03[3297:303] 1RegularExpression03[3297:303] 2RegularExpression03[3297:303] 3RegularExpression03[3297:303] .RegularExpression03[3297:303] cRegularExpression03[3297:303] oRegularExpression03[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.length)];        if(resultArray.count){            for (NSTextCheckingResult* result in resultArray) {                NSLog(@"%@",[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] .
0 0
原创粉丝点击