iOS 正則匹配URL

`+ (NSString *)distinguishWebsiteStr:(NSString *)sourceStr{ NSString *targetStr = nil;ui

NSError *error;
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:sourceStr options:0 range:NSMakeRange(0, [sourceStr length])];

if (arrayOfAllMatches.count > 0) {
    NSTextCheckingResult *match = [arrayOfAllMatches firstObject];
    targetStr = [sourceStr substringWithRange:match.range];
    
}
return targetStr;

// for (NSTextCheckingResult *match in arrayOfAllMatches) { // targetStr = [sourceStr substringWithRange:match.range]; // } }`code

相關文章
相關標籤/搜索