Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 546
iPhone NewBIe Avatar asked Dec 21 '25 05:12

iPhone NewBIe


1 Answers

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];
}
like image 199
Paras Joshi Avatar answered Dec 22 '25 20:12

Paras Joshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!