is Youtube Url by regex - Android java.util.regex

Android examples for java.util.regex:URL Pattern

Description

is Youtube Url by regex

Demo Code


//package com.java2s;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static boolean isYoutubeUrl(String youtubeUrl) {
        boolean isValid = false;
        if (youtubeUrl != null && youtubeUrl.trim().length() > 0
                && youtubeUrl.startsWith("http")) {
            String expression = "^.*((youtube"
                    + "\\/)"
                    + "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";
            CharSequence input = youtubeUrl;
            Pattern pattern = Pattern.compile(expression,
                    Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(input);
            if (matcher.matches()) {
                isValid = true;//from www .  ja v  a2s.  co m
            }
        }
        return isValid;
    }
}

Related Tutorials