-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?

1
  • 1
    Did your code even compile? Commented Oct 6, 2013 at 23:21

3 Answers 3

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)); 
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.