2012-12-05 31 views
3

我在验证URL发送我的代码以服务已经strucked,任何一个可以给我的正则表达式,用于验证应与http开头的网址://,应.COM结束.ORG.NET埃杜。在在url.i其他一些常见的结局已经搜查了许多,但我不力得到准确的这种类型的常规expression.Please帮我在这。提前致谢。验证网址完全相同

回答

10

试试这个方法...

- (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]; 
} 
+1

@iPhoneNewBle试试​​这个方法,还可以使用正则表达式可选这段代码的NSString *正则表达式=“(HTTP | FTP | HTTPS):// [\ W-_] + (。[\ w -_] +)+([\ w - 。,@?^ =%&:/〜+#] * [\ w - \ @?^ =%&/〜+#])? ;我希望这可以帮助你.. :) –

+1

我得到了它的第一个注册exp.once再次感谢很多快速帮助。 –

+0

hi @Paras Joshi,你可以提供reg exp为“http://xcode.blogspot.in/”类型的url吗?上面的reg exp不验证this.please帮助我吗? –