使用正则提取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 = [NSRegularExpressionregularExpressionWithPattern:regulaStroptions:NSRegularExpressionCaseInsensitiveerror:&error];

NSLog(@"error:%@", error);

NSArray*arrayOfAllMatches = [regexmatchesInString:stringoptions:NSMatchingReportCompletionrange:NSMakeRange(0, [stringlength])];

for(NSTextCheckingResult*matchinarrayOfAllMatches)

{

NSString* substringForMatch = [stringsubstringWithRange:match.range];

NSLog(@"substringForMatch");

NSLog(@"%@", substringForMatch);

}



判断有没有加http开头:

if ([self.urlString rangeOfString:@"http"].location == NSNotFound) {

self.urlString = [@"http://" stringByAppendingString:self.urlString];

}

你可能感兴趣的:(使用正则提取url(iOS))