java - 如何用正则提取html内容
问题描述
<p class='info-detail-head-classify-subname'><a href='https://www.haobala.com/wenda/11492.html' target='_blank'>财富</a></p> 想用java 提取财富两个字 请问用正则怎么提取 用jsoup会不会简单一点
问题解答
回答1:可以使用jsoup和regex, 推荐使用jsoup!jsoup document:https://jsoup.org/cookbook/in...http://www.open-open.com/jsoup/
import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element;import java.util.regex.Matcher; import java.util.regex.Pattern;public class Main { public static void main(String[] args) {// 方法1: jsoup String html = '<p class='info-detail-head-classify-subname'><a href='https://www.haobala.com/wenda/11492.html' target='_blank'>财富</a></p>';Document doc = Jsoup.parse(html); Element element = doc.getElementById('info_detail_head_classify_type'); System.out.println(element.text());// 方法2: regex Pattern r = Pattern.compile('<a.*>(.*)</a>'); Matcher m = r.matcher(html); if (m.find()) {System.out.println(m.group(1)); }} }回答2:
<a[^>]*>([^<]*)</a>
取<a></a>中的内容
相关文章:
1. Discuz! Q 有人用过吗?2. 怎么用navicat for mysql连接别的电脑的数据库?3. java enum 变量不能再case里面重复定义?4. javascript - vue2.0中使用vue2-dropzone的demo,vue2-dropzone的github网址是什么??百度不到。5. mysql根据输入的字段数据获取到ID怎么解决??6. Python, for-else, while-else是否造成了语义歧义 ( 增加心智负担 )?7. javascript - webpack中alias配置中的“@”是什么意思?8. python打包代码上传PYPI出现错误500,虚心求教9. :not 选择器 无效果 原因何在?10. 为啥最大化个窗口还得找一堆理由?

网公网安备