iphoneregexurl-validation

Validate URL exactly


i have strucked in validating URL to send to service in my code,can any one give me regular expression for validating URL that should start with http:// and should ends with .com , .org ,.net ,.edu ,.in some other common endings in the url.i have searched many but i dint get accurately for this type regular expression.Please help me in this. Thanks in Advance.


Solution

  • try this method...

      - (BOOL) urlIsValiad: (NSString *) url 
     {
        NSString *regex = 
        @"((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?";
       /// OR use this 
       ///NSString *regex = "(http|ftp|https)://[\w-_]+(.[\w-_]+)+([\w-.,@?^=%&:/~+#]* [\w-\@?^=%&/~+#])?";
        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];
    }