使用正则提取url(iOS)
直接上代码:
NSString *string = @"我是大大 www.baidu.com 咪咪咪";
NSError *error;
NSString *regulaStr = @"\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>???“”‘’]))";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr options:NSRegularExpressionCaseInsensitive error:&error];
NSLog(@"error:%@", error);
NSArray *arrayOfAllMatches = [regex matchesInString:string options:NSMatchingReportCompletion range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in arrayOfAllMatches)
{
NSString* substringForMatch = [string substringWithRange:match.range];
NSLog(@"substringForMatch");
NSLog(@"%@", substringForMatch);
}