Creating java regex to get href link

大城市里の小女人 提交于 2019-11-29 15:51:23

问题


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!


回答1:


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.




回答2:


If you always have one such link in a string, try this:

"(^[^\"]*\")|(\"[^\"]*)$"



回答3:


you can checkout http://regexlib.com/ for all the regex help you need. And the one below is for url :

^[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)$


来源:https://stackoverflow.com/questions/8307839/creating-java-regex-to-get-href-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!