1. 正则表达式判断url
NSString *a = @"http+:[^\\s]*";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", a];
BOOL checked = [emailTest evaluateWithObject:urlString];
下面是判断url合法并抽取合法的url:
//NSRegularExpression类里面调用表达的方法需要传递一个NSError的参数。下面定义一个
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])];
NSTextCheckingResult *firstMacth = [regex ]
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
//从urlString当中截取数据
NSString *result=[urlString substringWithRange:resultRange];
//输出结果
NSLog(@"%@",result);
}else {
NSLog(@"no result");
}
}