java - 如何用正則提取html內(nèi)容
問題描述
<p class='info-detail-head-classify-subname'><a href='http://www.baoyu77737.com/wenda/11492.html' target='_blank'>財(cái)富</a></p> 想用java 提取財(cái)富兩個(gè)字 請(qǐng)問用正則怎么提取 用jsoup會(huì)不會(huì)簡(jiǎn)單一點(diǎn)
問題解答
回答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.baoyu77737.com/wenda/11492.html' target='_blank'>財(cái)富</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>中的內(nèi)容
相關(guān)文章:
1. objective-c - 微信快捷發(fā)送最近一張圖片是如何實(shí)現(xiàn)的?2. PHP單例模式3. mysql 5萬張表 導(dǎo)出成sql 不要內(nèi)容,只要結(jié)構(gòu),非常慢。如何解決啊?4. mysql - eclispe無法打開數(shù)據(jù)庫連接5. 數(shù)據(jù)庫 - mysql中有沒查看數(shù)據(jù)大小的函數(shù)??6. mysql無法刪除字段(錯(cuò)誤1091),但是對(duì)該字段設(shè)置主鍵后就可刪除,為什么?7. 老師 我是一個(gè)沒有學(xué)過php語言的準(zhǔn)畢業(yè)生 我希望您能幫我一下8. mysql如何配置遠(yuǎn)程php外網(wǎng)鏈接數(shù)據(jù)庫9. 導(dǎo)入數(shù)據(jù)庫不成功10. mysql如何判斷數(shù)據(jù)不存在則插入呢?
