gpt4 book ai didi

java - RegEx 从字符串中提取第二个 URL

转载 作者:行者123 更新时间:2023-11-30 11:07:49 25 4
gpt4 key购买 nike

我正在尝试像这样从 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>

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

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

关于java - RegEx 从字符串中提取第二个 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28752814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com