标签:
RTLabel和RegexKitLite都要导入第三方库
使用Regexkitlite库进行正则表达式的解析
1.库是使用MRR,如果在ARC工程里面使用这个类,必须在project->build phases->compile resources里面将regexKitLite.m的编译指令设为:-fno-objc-arc
2.需要添加一个依赖库:libicucore.tbd
1.RTLable 富文本类库 替换成超链接
RTLabel *lable = [[RTLabel alloc]initWithFrame:CGRectMake(10, 100, 300, 0)];
lable.delegate = self;
NSString * string = @"@张三123:正则表达式好学么,@李四:给你一个网站:http://www.baidu.com/image/2323_303.jpg,#学习#,电话号码:(025)-69716261";
lable.text =string;
CGSize size =[lable optimumSize]; //计算尺寸
lable.frame = CGRectMake(10, 100, size.width, size.height);
NSString *regex = @"(@\\w+)|(#\\w+#)|(http(s)?://[0-9a-zA-Z./_]+)"; //正则表达式
NSArray *result = [string componentsMatchedByRegex:regex];
for (NSString * str in result) {
NSString * newKeyword = [NSString stringWithFormat:@"<a href=‘http://www.baidu.com‘>%@</a>",str];
string = [string stringByReplacingOccurrencesOfString:str withString:newKeyword]; //替换文本
}
lable.text = string;
2.正则表达式
NSString * str = @"@张三123:你在哪里呀?@李四:我在这!#在哪里# http://www.baidu.com 电话号码:(025)88888888,025-88888888";
NSString * regex = @"@\\w+";
NSString * regex = @"#\\w+#";
NSString * regex = @"(@\\w+)|(#\\w+#)";
NSString * regex = @"http(s)?://[0-9a-zA-Z./_]+";网址
NSString * regex = @"(\\()?(\\d){3}(\\))?(-)?(\\d){8}";//电话号码
NSArray * result = [str componentsMatchedByRegex:regex];
for (NSString * str in result) {
NSLog(@"%@",str);
}
标签:
原文地址:http://www.cnblogs.com/huoran1120/p/5139930.html