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.

Ramesh
  • 2,295
  • 5
  • 35
  • 64

4 Answers4

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));
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
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.

henull7
  • 15
  • 1
  • 6
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("&"));
jdesai927
  • 62
  • 1
  • 1
  • 5
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();
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89