Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static String getYoutubeVideoId(String url) { Pattern pattern = Pattern.compile("^https?://.*(?:youtu.be/|v/|u/\\w/|embed/|watch?v=)([^#&?]*).*$", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(url); if (!matcher.matches()) throw new IllegalArgumentException("Regex fail. not matched. Url: " + url); return matcher.group(1); } }