Reputation: 61
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
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
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
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 thesewww.
or m.
or none of theseyoutube.<anyTLD>
or youtu.be
/
playlist
, watch?
, channel
or user
a-zA-Z0-9
and =
more than 1 timeThis 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
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