字符串中提取url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
NSError *error;
//可以识别url的正则表达式
NSString *regulaStr = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\.\-]+\.([a-zA-Z]{2,4})(:\d+)?(/[a-zA-Z0-9\.\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\.\-]+\.([a-zA-Z]{2,4})(:\d+)?(/[a-zA-Z0-9\.\-~!@#$%^&*+?:_/=<>]*)?)";

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
options:NSRegularExpressionCaseInsensitive
error:&error];

NSArray *arrayOfAllMatches = [regex matchesInString:pasteUrl options:0 range:NSMakeRange(0, [pasteUrl length])];

NSMutableArray *arr=[[NSMutableArray alloc] init];

for (NSTextCheckingResult *match in arrayOfAllMatches){
NSString* substringForMatch;
substringForMatch = [pasteUrl substringWithRange:match.range];
[arr addObject:substringForMatch];
}