创建java正则表达式来获取href链接 [英] Creating java regex to get href link
问题描述
很抱歉,如果以前有人问过这个问题,但我在网上找不到任何答案。我很难找出与此正则表达式相反的结果:
\[^>] * \>
我想使用replaceAll替换除链接之外的所有内容。所以如果我有类似这样的标签:
< p>< a href =http: //www.google.com\">Google</a></p>
lockquote
s.replaceAll(regex,);
/ blockquote>
给我这个输出:
http: //www.google.com
我知道有更好的方法可以做到这一点,但我有使用正则表达式。任何帮助真的很感激,谢谢!
您不必使用 replaceAll
。更好地使用如下模式组:
模式p = Pattern.compile(href = \(。*?) \ );
Matcher m = p.matcher(html);
String url = null;
if(m.find()){
url = m.group(1); //这个变量应该包含链接URL
}
如果您有多个链接HTML在循环中执行 m.find()
。
Sorry if this has been asked before, but I couldn't find any answers on the web. I'm having a hard time figuring out the inverse to this regex:
"\"[^>]*\">"
I want to use replaceAll to replace everything except the link. So if I had a tag similar to this:
<p><a href="http://www.google.com">Google</a></p>
I need a regex that would satisfy this:
s.replaceAll(regex, "");
to give me this output:
http://www.google.com
I know there are better ways to do this, but I have to use a regex. Any help is really appreciated, thanks!
You do not have to use replaceAll
. Better use pattern groups like the following:
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
}
If you have several links into your HTML perform m.find()
in loop.
这篇关于创建java正则表达式来获取href链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!