Android / Java: check if youtube url is correct

I want to check if the url is the youtube url so that I can show it, otherwise I will hide the view.

Is there any regular expression in Java that can help me verify the URL is correct. I am currently using this regex, but I think this is 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
}
+5
source share
5 answers

You have to use

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

It will return True if the url is valid and false if the url is invalid.

+5
source

android.webkit.URLUtil.isValidUrl(java.lang.String), URL-. , url Youtube.

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;
}
+3

URL-. "youtube" "youtu.be" URL-.

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

". *" , (youtube youtu.be), .

. URL

+1

, , :

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

youtubeUrl URL :

URL, youtube.com.

+1

.

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;
  }

YoutubeIid , .

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

Source: https://habr.com/ru/post/1543168/


All Articles