-3

I'm trying to write a simple java regular expression to extract out the video id of a given youtube video from its url. E.g for:

http://www.youtube.com/watch?v=-mzvAAuCo1c

I want to extract out: -mzvAAuCo1c.

Here's what I'm trying:

Pattern pattern = Pattern.compile("v=([^&]+)");
String url = "http://www.youtube.com/watch?v=-mzvAAuCo1c";
Matcher matcher = pattern.match(url);
System.out.println(matcher.getGroupCount() ); //outputs 1
System.out.println(matcher.matches() ); //returns false;
System.out.println( matcher.group(0) ); //throws exception, same for 1

What am I doing wrong?

Ali
  • 261,656
  • 265
  • 575
  • 769

3 Answers3

2

Invoke find to match the partial String. Dont call matches after calling find - this will result in an IllegalStateException. You want to capture group 1 rather than 0 as the latter returns the full String

Pattern pattern = Pattern.compile("v=([^&]+)");
String url = "http://www.youtube.com/watch?v=-mzvAAuCo1c&foo=3";
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
    System.out.println(matcher.groupCount()); 
    System.out.println(matcher.group(1)); 
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
2
Matcher matcher = pattern.match(url);
System.out.println(matcher.getGroupCount() ); //outputs 1

First, the two lines above do not even compile. Change them to:

Matcher matcher = pattern.matcher(url);
System.out.println(matcher.groupCount() ); //outputs 1

Second, your regular expression:

Pattern pattern = Pattern.compile("v=([^&]+)");

only matches part of the input, which is why

matcher.matches()

returns false. Change the regular expression to:

Pattern pattern = Pattern.compile(".*v=([^&]+)");

Finally, since matcher.matches() does not return true, the statement

matcher.group(0)

will throw an exception.

Fixed code:

        Pattern pattern = Pattern.compile(".*v=([^&]+)");
        String url = "http://www.youtube.com/watch?v=-mzvAAuCo1c";
        Matcher matcher = pattern.matcher(url);
        System.out.println(matcher.groupCount()); //outputs 1
        System.out.println(matcher.matches()); //returns true;
        System.out.println(matcher.group(1)); //returns -mzvAAuCo1c
Terry Li
  • 16,870
  • 30
  • 89
  • 134
0

Try this

(?<=videos\/|v=)([\w-]+)

In use

public static void main(String[] args) {

String link = "http://gdata.youtube.com/feeds/api/videos/xTmi7zzUa-M&whatever";
String pattern = "(?:videos\\/|v=)([\\w-]+)";

Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(link);

if(matcher.find()){
    System.out.println(matcher.group());
    }
}

Found Here.

Community
  • 1
  • 1
danmc
  • 1,172
  • 13
  • 11