正则表达式识别URL

前端之家收集整理的这篇文章主要介绍了正则表达式识别URL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

正则表达式:(来源: 
http://daringfireball.net/2010/07/improved_regex_for_matching_urls

NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"(?i)//b((?:[a-z][//w-]+:(?:/{1,3}|[a-z0-9%])|www//d{0,3}[.]|[a-z0-9.//-]+[.][a-z]{2,4}/)(?:[^//s()<>]+|//(([^//s()<>]+|(//([^//s()<>]+//)))*//))+(?://(([^//s()<>]+|(//([^//s()<>]+//)))*//)|[^//s`!()//[//]{};:'/".,<>?«»“”‘’]))" options:NSRegularExpressionCaseInsensitive error:NULL];  
NSString *someString = @"This is a sample of a http://abc.com/efg.PHP?EFAei687e3EsA sentence with a URL within it.";  
NSString *match = [someString substringWithRange:[expression rangeOfFirstMatchInString:someString options:NSMatchingCompleted range:NSMakeRange(0,[someString length])]];  
NSLog(@"%@",match); // Correctly prints 'http://abc.com/efg.PHP?EFAei687e3EsA' 

系统的NSDataDetector:

NSError* error = nil;
NSDataDetector* linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
[linkDetector enumerateMatchesInString:plainText options:0 range:NSMakeRange(0,[plainText length])
									usingBlock:^(NSTextCheckingResult *result,NSMatchingFlags flags,BOOL *stop){
	NSRange *range = [result range];
		} 
原文链接:https://www.f2er.com/regex/362246.html

猜你在找的正则表达式相关文章