iOS中系统自带正则表达式的应用
|
//组装一个字符串,把里面的网址解析出来
NSString *urlString = @"sfdshttp://www.baidu.com";
NSError *error;
//http+:[^\\s]* 这是检测网址的正则表达式
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"http+:[^\\s]*" options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
//从urlString中截取数据
NSString *result = [urlString substringWithRange:resultRange];
NSLog(@"%@",result);
}
}
|