Regex for url with port in Objective c
Here is the code I'm using, it validates 99% of the urls I give it, b开发者_如何学JAVAut it fails when there is a ":port" after an IP address in the URL.
-(BOOL) validateUrl: (NSString *) candidate {
//NSString *urlRegEx=@"(http|https)://(((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+)|(";
NSString *urlRegEx = @"^(http|https|ftp)\://(([a-zA-Z0-9-.]+\.[a-zA-Z]{2,3})|([0-2]*\d*\d\.[0-2]*\d*\d\.[0-2]*\d*\d\.[0-2]*\d*\d))(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9-._\?\,\'/\+&%\$#\=~])*[^.\,)(\s]$";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
thanks for the help!
Why not use NSURL
? Try creating a URL with URLWithString:
and if it returns nil, your URL was malformed. If you don’t get nil in return, then you can check the host, port, etc.
精彩评论