正则表达式-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
- 正则表达式-03 更多的方式
- 正则表达式-02 更多的方式
- 更多提高正则表达式效率的方法
- 一个涉及比较多的正则表达式, 希望可以帮助大家对正则有更多了解.
- 使用正则表达式的方式重写URL。
- 正则表达式的两种创建方式
- iOS 正则表达式的使用方式
- 正则表达式的两种创建方式
- swift正则表达式的几种方式
- 正确的表白方式(正则表达式)
- js中使用正则表达式(一)创建正则表达式的方式:正则对象方式和正则字面量方式
- 正则表达式匹配方式
- Perl——正则表达式(三) 更多匹配 //g
- (正则表达式)Regex类的一些常见使用方式
- (正则表达式)Regex类的一些常见使用方式
- 正则表达式中一些常见的使用方式
- JavaScript 正则表达式两种使用方式的区别
- 正则表达式使用方式以及以及简单的例子
- [ios] 实际开发中的小细节
- Git常用命令汇总
- CString 转为 const char*
- Unity创建项目及基本面板介绍
- Android的Sqlite中DateTime类型数据的存取问题
- 正则表达式-03 更多的方式
- 堆和栈的区别Tag
- HDU 2795 Billboard 线段树
- HDU 5112 A Curious Matt (14北京 排序 水题)
- Flume-ng的原理和使用
- U3d脚本注意事项及两个基本函数的简单介绍
- adb shell命令
- 输出素数
- 跟我从零基础学习Unity3D开发-NGUI入门基础