3

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.

iPhone NewBIe
  • 407
  • 1
  • 7
  • 16

1 Answers1

10

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];
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • 1
    @iPhoneNewBle try this method and also use regex optional this code NSString *regex = "(http|ftp|https)://[\w-_]+(.[\w-_]+)+([\w-.,@?^=%&:/~+#]* [\w-\@?^=%&/~+#])?"; i hope this help you.. :) – Paras Joshi Dec 05 '12 at 06:04
  • 1
    i got it with the first reg exp only.once again thanks alot for quick help. – iPhone NewBIe Dec 05 '12 at 06:11
  • hi @Paras Joshi, can u give reg exp for "http://xcode.blogspot.in/" type of url?the above reg exp is not validating this.please help me in this? – iPhone NewBIe Dec 05 '12 at 06:22
  • hey here your this link not found the page dude so this happen i check the link but Page Not Found here – Paras Joshi Dec 05 '12 at 06:25