use*_*866 1 html java regex href inverse
很抱歉,如果之前已经询问过,但我在网上找不到任何答案.我很难搞清楚这个正则表达式的反转:
"\"[^>]*\">"
我想使用replaceAll来替换除链接之外的所有内容.所以如果我有一个类似这样的标签:
<p><a href="http://www.google.com">Google</a></p>
我需要一个满足这个要求的正则表达式:
s.replaceAll(regex,"");
给我这个输出:
http://www.google.com
我知道有更好的方法可以做到这一点,但我必须使用正则表达式.非常感谢任何帮助,谢谢!
Ale*_*exR 14
你不必使用replaceAll.更好地使用模式组,如下所示:
Pattern p = Pattern.compile("href=\"(.*?)\"");
Matcher m = p.matcher(html);
String url = null;
if (m.find()) {
url = m.group(1); // this variable should contain the link URL
}
Run Code Online (Sandbox Code Playgroud)
如果您有几个链接到您的HTML执行m.find()循环.