0

I am extracting a youtube video id from a youtube link. the list looks like this

http://www.youtube.com/watch?v=mmmc&feature=plcp

I want to get the mmmc only.

i used .replaceAll ?

Dimitri
  • 1,924
  • 9
  • 43
  • 65
  • 1
    Examine Regular Expressions - see the sun tutorials http://docs.oracle.com/javase/tutorial/essential/regex/index.html – Katana24 Dec 10 '12 at 09:52
  • did you try request.getParameter("v"); in servlet ? it will give you mmmc directly. that is ofcourse if you use servlets – Bhavik Shah Dec 10 '12 at 09:56
  • 1
    It's worth looking at http://stackoverflow.com/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url?rq=1, which has some pretty well-developed answers for javascript – RoryB Dec 10 '12 at 09:58

4 Answers4

3

Three ways:

  1. Url parsing: http://download.oracle.com/javase/6/docs/api/java/net/URL.html
URL url = new URL("http://www.youtube.com/watch?v=mmmc&feature=plcp");
url.getQuery(); // return query string.
  1. Regular Expression Examples here http://www.vogella.com/articles/JavaRegularExpressions/article.html

  2. Tokenize

    String s = "http://www.youtube.com/watch?v=mmmc&feature=plcp";
    String arr[] = s.split("=");
    String arr1[] = arr[1].split("&");
    System.out.println(arr1[0]);
    
Ravi Bhatt
  • 3,147
  • 19
  • 21
1

If you'd like to use regular expressions, this could be a solution:

Pattern p = Pattern
.compile("http://www.youtube.com/watch\\?v=([\\s\\S]*?)\\&feature=plcp");

Matcher m = p.matcher(youtubeLink);

if (m.find()) {
return m.group(1);
}
else{
throw new IllegalArgumentException("invalid youtube link");
}

Of course, this will only work if the feature will always be plcp, if not, you could simply remove that part or replace it with a wilcard as I did with mmmc

Qkyrie
  • 921
  • 7
  • 10
0

Edit: now i know what you are looking for i hope:

String url= "http://www.youtube.com/watch?v=mmmc&feature=plcp";
String search = "v=";
int index     = url.indexOf(search);
int index2    = url.indexOf("&",index);
String found  = url.substring(index+2,index2);
System.out.println(found);
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
0

Here's a generic solution (using Guava MapSplitter):

public final class UrlUtil {
    /**
     * Query string splitter.
     */
    private static final MapSplitter PARAMS_SPLITTER = Splitter.on('&').withKeyValueSeparator("=");

    /**
     * Get param value in provided url for provided param.
     * 
     * @param url Url to use
     * @param param Param to use
     * @return param value or null.
     */
    public static String getParamVal(String url, String param)
    {
        if (url.contains("?")) {
            final String query = url.substring(url.indexOf('?') + 1);
            return PARAMS_SPLITTER.split(query).get(param);
        }

        return null;
    }

    public static void main(final String[] args)
    {
        final String url = "http://www.youtube.com/watch?v=mmmc&feature=plcp";
        System.out.println(getParamVal(url, "v"));
        System.out.println(getParamVal(url, "feature"));
    }
}

Outputs:

mmmc
plcp