java - 如何用正則提取html內容
問題描述
<p class='info-detail-head-classify-subname'><a href='http://www.leifengta.com.cn/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='http://www.leifengta.com.cn/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. boot2docker無法啟動2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. docker-compose中volumes的問題4. docker安裝后出現Cannot connect to the Docker daemon.5. nignx - docker內nginx 80端口被占用6. java - SSH框架中寫分頁時service層中不能注入分頁類7. javascript - mock.js可以存儲數據嗎8. docker api 開發的端口怎么獲???9. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?10. golang - 用IDE看docker源碼時的小問題

網公網安備