Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RegEx从字符串中复制第二个URL_Java - Fatal编程技术网

Java RegEx从字符串中复制第二个URL

Java RegEx从字符串中复制第二个URL,java,Java,我正试图从这些Stings中提取第二个url submitted by <a href="http://www.reddit.com/user/thecrappycoder"> thecrappycoder </a> <br /> <a href="http://blogs.msdn.com/b/bethmassi/archive/2015/02/25/understanding-net-2015.aspx">[link]</a> &

我正试图从这些Stings中提取第二个url

 submitted by <a href="http://www.reddit.com/user/thecrappycoder"> thecrappycoder </a> <br /> <a href="http://blogs.msdn.com/b/bethmassi/archive/2015/02/25/understanding-net-2015.aspx">[link]</a> <a href="http://www.reddit.com/r/programming/comments/2x9o4o/understanding_net_2015/">[3 comments]</a>
 submitted by <a href="http://www.reddit.com/user/durdn"> durdn </a> <br /> <a href="https://www.youtube.com/watch?v=yG-UaBJXZ80">[link]</a> <a href="http://www.reddit.com/r/programming/comments/2x89le/hacking_with_andrew_and_brad_an_http2_client/">[1 comment]</a>
我也试过这样做

System.out.println(("http://"+text.split("http://")[1]).split("")[0]);

不幸的是,我没能得到它。任何帮助,谢谢。

您可以使用简化的正则表达式模式采用相同的方法:

String text = "submitted by <a href=\"http://www.reddit.com/user/thecrappycoder\"> thecrappycoder </a> <br />" +
        " <a href=\"http://blogs.msdn.com/b/bethmassi/archive/2015/02/25/understanding-net-2015.aspx\">[link]</a> " +
        "<a href=\"http://www.reddit.com/r/programming/comments/2x9o4o/understanding_net_2015/\">[3 comments]</a>\n" +
        " ";
String regex = "href=.(http.*?)\"";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
m.find(); // ignore the 1st match
m.find(); // find the 2nd match
String urlStr = m.group(); // read the 2nd match
System.out.println("urlStr = " + urlStr); // prints: urlStr = http://blogs.msdn.com/b/bethmassi/archive/2015/02/25/understanding-net-2015.aspx
String text=“提交人
”+ " " + “\n”+ " "; 字符串regex=“href=(http.*?\”; Pattern p=Pattern.compile(regex); 匹配器m=p.Matcher(文本); m、 find();//忽略第一个匹配项 m、 find();//查找第二个匹配项 字符串urlStr=m.group();//读取第二个匹配项 System.out.println(“urlStr=“+urlStr);//prints:urlStr=http://blogs.msdn.com/b/bethmassi/archive/2015/02/25/understanding-net-2015.aspx
谢谢你的优雅解决方案。我不知道这样做可以忽略第一场比赛。谢谢!
String text = "submitted by <a href=\"http://www.reddit.com/user/thecrappycoder\"> thecrappycoder </a> <br />" +
        " <a href=\"http://blogs.msdn.com/b/bethmassi/archive/2015/02/25/understanding-net-2015.aspx\">[link]</a> " +
        "<a href=\"http://www.reddit.com/r/programming/comments/2x9o4o/understanding_net_2015/\">[3 comments]</a>\n" +
        " ";
String regex = "href=.(http.*?)\"";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
m.find(); // ignore the 1st match
m.find(); // find the 2nd match
String urlStr = m.group(); // read the 2nd match
System.out.println("urlStr = " + urlStr); // prints: urlStr = http://blogs.msdn.com/b/bethmassi/archive/2015/02/25/understanding-net-2015.aspx