Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for url with port in Objective c

Here is the code I'm using, it validates 99% of the urls I give it, but 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!

like image 556
Lance Avatar asked Mar 07 '11 21:03

Lance


1 Answers

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.

like image 57
Todd Yandell Avatar answered Oct 05 '22 23:10

Todd Yandell