1

我正在尝试从 url 中提取视频 ID

http://www.youtube.com/v/Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6

我用什么我Pattern.compile("/v/([^&]*)")得到空,如果我用Pattern.compile("/v/([^/]*)")我得到下面的输出 Ig1WxMI9bxQ&hl=fr&fs=1&color1=0x2b405b&color2=0x6b8ab6

我怎样才能Ig1WxMI9bxQ单独提取。

4

4 回答 4

1

要找到 id,您可以使用:

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));
}
于 2012-07-27T09:48:30.247 回答
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();
于 2012-07-27T09:52:17.567 回答
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");
    }
  }
}

您可以使用 txt2re.com 来满足您的所有正则表达式需求(:值得检查。没用的,但您当然可以简化您获得的来源。

于 2012-07-27T09:47:46.353 回答
0

尝试:

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("&"));
于 2012-07-27T09:48:30.857 回答