i use this function to check if an URL is valid or not. it works for most of the cases, but here the following URL it return that is not Valid!
http://www.iphonedevsdk.com/forum/iphone-sdk-development/11841-stringbytrimmingcharactersinset.html
what can i do to improve the regular expression of my function that it will cover all URL types?
- (BOOL) urlIsValiad: (NSString *) url
{
NSString *regex =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([regextest evaluateWithObject: url] == YES) {
NSLog(@"URL is valid!");
} else {
NSLog(@"URL is not valid!");
}
return [regextest evaluateWithObject:url];
}
This would be my attempt:
((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?
EDIT
Added in a back-reference (?<=/)
for extra specificity
EDIT 2 Added optional search for country name in URL (eg www.google.com.in)