? ?? 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);
? ? }