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 ?
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 ?
 
    
    Three ways:
URL url = new URL("http://www.youtube.com/watch?v=mmmc&feature=plcp"); url.getQuery(); // return query string.
Regular Expression Examples here http://www.vogella.com/articles/JavaRegularExpressions/article.html
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]);
 
    
    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
 
    
    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);
 
    
    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