1

我正在尝试验证 youtube 网址 http://www.youtube.com/watch?v=UdQfR4nsXvI 。使用下面的代码

(BOOL) validateUrl: (NSString *) candidate
{

NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";

NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];

return [urlTest evaluateWithObject:candidate];

} 

我还想验证任何 youtube URL 格式....

编辑代码:


- (IBAction)uplaod_video_Action:(id)sender
{
    NSURL *candidateURL = [NSURL URLWithString:youtube_Url.text];

    // WARNING > "test" is an URL according to RFCs, being just a path
    // so you still should check scheme and all other NSURL attributes you need

    if (candidateURL && candidateURL.scheme && candidateURL.host)
    {
        // candidate is a well-formed url with:
        //  - a scheme (like http://)
        //  - a host (like stackoverflow.com)
        NSLog(@"hallo");
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Please enter the Email" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];

    }
4

3 回答 3

4

使用regexpal.com检查您的验证机制

在此处输入图像描述

Raywenderlich.com 上有一个非常全面的正则表达式教程

于 2013-05-16T10:32:16.953 回答
0

试试用这个

-(BOOL) validateUrl: (NSString *) candidate
{

NSString *urlRegEx = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";

NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];

return [urlTest evaluateWithObject:candidate];

} 
于 2013-05-16T10:35:29.333 回答
0

尝试这个

NSString *urlRegEx = @"(^http:\/\/(?:www\.)?youtube.com\/watch\?(?=[^?]*v=\w+)(?:[^\s?]+)?$);
于 2013-05-16T10:32:04.657 回答