我正在尝试从像这样的St中提取第二个网址

 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>


通过使用正则表达式。我试过了

String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&amp;@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&amp;@#/%=~_()|]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while(m.find()) {
    String urlStr = m.group();
    urlStr = urlStr.substring(1, 3);
        links.add(urlStr);
}


我也尝试过

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

07-24 13:04