8

I want to check if an url is valid youtube url so that I can shown in view otherwise I will hide the view.

Is there any regular expression in Java that can help me to check if url is valid. Currently I am using this regex but I guess it's not the one I want:

String youTubeURl = "https://www.youtube.com/watch?v=Btr8uOU0BkI";
String pattern = "https?:\\/\\/(?:[0-9A-Z-]+\\.)?(?:youtu\\.be\\/|youtube\\.com\\S*[^\\w\\-\\s])([\\w\\-]{11})(?=[^\\w\\-]|$)(?![?=&+%\\w]*(?:['\"][^<>]*>|<\\/a>))[?=&+%\\w]*";
 if (!youTubeURl.isEmpty() && youTubeURl.matches(pattern)) {
              /// Valid youtube URL
  }
 else{
   // Not Valid youtube URL
}
1
  • 1
    try to make http-connection with url and check response code Commented Jun 4, 2014 at 6:54

5 Answers 5

13

This works for me.

public static boolean isYoutubeUrl(String youTubeURl)
{
       boolean success;
       String pattern = "^(http(s)?:\\/\\/)?((w){3}.)?youtu(be|.be)?(\\.com)?\\/.+";
       if (!youTubeURl.isEmpty() && youTubeURl.matches(pattern))
       {
           success = true;
       }
       else
       {
           // Not Valid youtube URL
           success = false;
       }
       return success;
  }

If you want to retrieve the Youtube videoId you can use the following function.

public static String getVideoIdFromYoutubeUrl(String youtubeUrl)
{
       /*
           Possibile Youtube urls.
           http://www.youtube.com/watch?v=WK0YhfKqdaI
           http://www.youtube.com/embed/WK0YhfKqdaI
           http://www.youtube.com/v/WK0YhfKqdaI
           http://www.youtube-nocookie.com/v/WK0YhfKqdaI?version=3&hl=en_US&rel=0
           http://www.youtube.com/watch?v=WK0YhfKqdaI
           http://www.youtube.com/watch?feature=player_embedded&v=WK0YhfKqdaI
           http://www.youtube.com/e/WK0YhfKqdaI
           http://youtu.be/WK0YhfKqdaI
        */
       String pattern = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
       Pattern compiledPattern = Pattern.compile(pattern);
       //url is youtube url for which you want to extract the id.
       Matcher matcher = compiledPattern.matcher(youtubeUrl);
       if (matcher.find()) {
           return matcher.group();
       }
       return null;
}
Sign up to request clarification or add additional context in comments.

Comments

6

You should use

Patterns.WEB_URL.matcher(youTubeURl).matches()

It will return True if URL is valid and false if URL is invalid.

Comments

3

Use android.webkit.URLUtil.isValidUrl(java.lang.String) to check if url is valid. And then you can check if url contains Youtube string.

Like

private boolean isValidUrl(String url) {

    if (url == null) {
        return false;
    }
    if (URLUtil.isValidUrl(url)) {
        // Check host of url if youtube exists
        Uri uri = Uri.parse(url);
        if ("www.youtube.com".equals(uri.getHost())) {
            return true;
        }
        // Other way You can check into url also like 
        //if (url.startsWith("https://www.youtube.com/")) {
            //return true;
        //}
    }
    // In other any case
    return false;
}

2 Comments

what about " youtu.be/zuf8A0udHrs " in this case? and i having trouble in fetching YouTube Video Id from url.
This is not a valid youtube url.
3

In order to achieve what you want you should use this Regex like so:

private static final Pattern youtubePattern = Pattern.compile("^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+");
private boolean isValid = youtubePattern.matcher(youtubeUrl).matches();

where youtubeUrl can be any URL of the following list:

This regex can match any types of URLs related to youtube.com

3 Comments

Some of the URLs are dead!
Yes, but the aim is the same, it validate whether or not the URL is from Youtube
@RobertoOsorioMcNulty regex in the string should be "^(http(s)?:\\/\\/)?((w){3}.)?youtu(be|.be)?(\\.com)?\\/.+"
1

This doesn't check if URL is valid or not. It only checks for strings "youtube" & "youtu.be" in your URL. Following is the regex

String yourUrl = "https://youtu.be/Xh0-x1RFEOY";
yourUrl.matches(".*(youtube|youtu.be).*")

" .* " at beginning & end means that there could be anything on left & right of the expression(youtube & youtu.be) you are checking.

NOTE: This has nothing to do with the validity of the URL

1 Comment

This will not work if you have "youtube" or "youtu.be" words in url. For example, link to this post also returns true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.