判断字符串是否是URL
发布网友
发布时间:2023-08-05 01:03
我来回答
共1个回答
热心网友
时间:2024-11-16 18:34
- (BOOL)isUrl
{
if(self == nil)
return NO;
NSString *url;
if (self.length>4 && [[self substringToIndex:4] isEqualToString:@"www."]) {
url = [NSString stringWithFormat:@"http://%@",self];
}else{
url = self;
}
NSString *urlRegex = @"(https|http|ftp|rtsp|igmp|file|rtspt|rtspu)://((((25[0-5]|2[0-4]\\d|1?\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1?\\d?\\d))|([0-9a-z_!~*'()-]*\\.?))([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\.([a-z]{2,6})(:[0-9]{1,4})?([a-zA-Z/?_=]*)\\.\\w{1,5}";
NSPredicate* urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegex];
return [urlTest evaluateWithObject:url];
}