[英]How to extract the String URL label and retrieve parent and grandparent path
[英]How to extract url and label from String <a> tag?
我有一个字符串格式为<a href="http://example.com">Example</a>
的<a>
标签列表。 从此字符串中提取URL http://example.com
和标签Example
的最佳方法是什么。 当前,我使用子字符串方法来标识边界并获取url和标签。 但是,使用正则表达式还有更好的方法吗?
也许您会选择一个好的HTML解析器。 即JSoup。
String html = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
Document doc = Jsoup.parse(html);
Element link = doc.select("a").first();
String text = doc.body().text(); // "An example link"
String linkHref = link.attr("href"); // "http://example.com/"
String linkText = link.text(); // "example""
String linkOuterH = link.outerHtml();
// "<a href="http://example.com"><b>example</b></a>"
String linkInnerH = link.html(); // "<b>example</b>"
这是正则表达式:
“ \\\\”(。*?)\\“ \\”
尽管我建议您使用特定于HTML属性提取的工具。
您可以使用Apache commons substringBetween方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.