CodeGuru
CodeGuru

Reputation: 61

Youtube URL validation ios

I am using this method to validate youtube url but it's not working.

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

    NSString *urlRegEx = @"(http://youtu.be/[a-zA-Z0-9_-]+)";


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

    return [urlTest evaluateWithObject:candidate];

}

-(BOOL) validateUrl1: (NSString *) candidate1
{


    NSString *urlRegEx1 = @"(https://(www|m){0,1}.youtube.com/(watch|playlist)[?]v=[a-zA-Z0-9_-]+)";


    urlTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx1];


    return [urlTest1 evaluateWithObject:candidate1];

}

Even if I edit the url and make it y.be instead of youtu.be, still these methods are returning YES

. Kindly tell me what's wrong in my code. If any one has a better RegEx please share that with me. I would also like to know how to write a RegEx.

Upvotes: 1

Views: 2684

Answers (4)

Ekramul Hoque
Ekramul Hoque

Reputation: 692

For Swift 5:

func isValidYouTubeLink(givenString: String) -> Bool {
        
        let youtubeRegex = "(http(s)?:\\/\\/)?(www\\.|m\\.)?youtu(be\\.com|\\.be)(\\/watch\\?([&=a-z]{0,})(v=[\\d\\w]{1,}).+|\\/[\\d\\w]{1,})"
        let youtubeCheckResult = NSPredicate(format: "SELF MATCHES %@", youtubeRegex)
        return youtubeCheckResult.evaluate(with: givenString)

    }

Upvotes: 0

Bojan Bozovic
Bojan Bozovic

Reputation: 910

If you want to check if String is the link to the youtube video (not the link for channel, or for embedding video):

func isYoutubeLink(checkString checkString: String) -> Bool {

let youtubeRegex = "(http(s)?:\\/\\/)?(www\\.|m\\.)?youtu(be\\.com|\\.be)(\\/watch\\?([&=a-z]{0,})(v=[\\d\\w]{1,}).+|\\/[\\d\\w]{1,})"

let youtubeCheckResult = NSPredicate(format: "SELF MATCHES %@", youtubeRegex)
return youtubeCheckResult.evaluateWithObject(checkString)
}

Upvotes: 3

kfairns
kfairns

Reputation: 3057

If it is regex that you are looking for, then perhaps you could try this:

(((?:https?:\/\/)?(?:www\.|m\.)?(?:youtube\.[a-z]{2,}|youtu\.be)(?:\/(?:playlist|watch\?|channel\/|user\/)[\w=]+)+)

It will match:

  • http://, https:// or none of these
  • www. or m. or none of these
  • youtube.<anyTLD> or youtu.be
  • /
  • playlist, watch?, channel or user
  • a string of characters from a-zA-Z0-9 and = more than 1 time

This should effectively match most urls on Youtube apart from the homepage, which if requested I could add in with a bit of tweaking.

You may have to add another escape, I haven't got much experience with objective-c

(((?:https?:\\/\\/)?(?:www\\.|m\.)?(?:youtube\\.[a-z]{2,}|youtu\\.be)(?:\\/(?:playlist|watch\\?|channel\\/|user\\/)[\\w=]+)+)

Here is an example of it working in Javascript, Python and PCRE:

https://regex101.com/r/sG1xP7/1

I hope this helps you

Upvotes: 0

dhiren bharadava
dhiren bharadava

Reputation: 271

    NSString *urlString = [NSString stringWithFormat:@"URL_STRING"];
    NSString *urlRegEx = @"(?:(?:\.be\/|embed\/|v\/|\\?v=|\&v=|\/videos\/)|(?:[\\w+]+#\\w\/\\w(?:\/[\\w]+)?\/\\w\/))([\\w-_]+)";
    NSPredicate *urlPredic = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
    BOOL isValidURL = [urlPredic evaluateWithObject:urlString];

if(isValidURL)
{
  // your URL is valid;
}
else
{
  // show alert message for invalid URL;
}

now please check let me know i'm waiting your replay it's work or not.

Upvotes: 0

Related Questions