1

I am trying to extract the video id from the url

http://www.youtube.com/v/Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6

What i use the Pattern.compile("/v/([^&]*)") i get null and if i use Pattern.compile("/v/([^/]*)") i get the below output Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6

how can i extract the Ig1WxMI9bxQ alone.

1

4 Answers 4

1

To locate the id you can use:

String url = "http://www.youtube.com/v/Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6";
Matcher matcher = Pattern.compile(".*/(.*?)=.*").matcher(url);
if (matcher.matches()) {
   System.out.println(matcher.group(1));
}
0
import java.util.regex.*;

class Main
{
  public static void main(String[] args)
  {
    String txt="http://www.youtube.com/v/Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6";

    String re1=".*?";   // Non-greedy match on filler
    String re2="(?:[a-z][a-z0-9_]*)";   // Uninteresting: var
    String re3=".*?";   // Non-greedy match on filler
    String re4="(?:[a-z][a-z0-9_]*)";   // Uninteresting: var
    String re5=".*?";   // Non-greedy match on filler
    String re6="(?:[a-z][a-z0-9_]*)";   // Uninteresting: var
    String re7=".*?";   // Non-greedy match on filler
    String re8="(?:[a-z][a-z0-9_]*)";   // Uninteresting: var
    String re9=".*?";   // Non-greedy match on filler
    String re10="(?:[a-z][a-z0-9_]*)";  // Uninteresting: var
    String re11=".*?";  // Non-greedy match on filler
    String re12="((?:[a-z][a-z0-9_]*))";    // Variable Name 1

    Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher m = p.matcher(txt);
    if (m.find())
    {
        String var1=m.group(1);
        System.out.print("("+var1.toString()+")"+"\n");
    }
  }
}

You can use txt2re.com to all your regex needs (: Its worth to check. Useless to mention, but ofcourse you can simplify the source you get.

0

Try:

String url = http://www.youtube.com/v/Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6
String id = url.substring(url.indexOf("/v/") + 3, url.indexOf("&"));
0
String link = "http://www.youtube.com/v/Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6";
Pattern rex = Pattern.compile("(?<=watch\\?v=|/videos/)[^&#]*");
Matcher m = rex.matcher(link);
String YouTubeVideoID = m.group();

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.