java - Comment extraire du contenu HTML à l'aide d'expressions régulières
習慣沉默
習慣沉默 2017-05-17 10:06:13
0
2
461
    <p class="info-detail-head-classify-subname"><a id="info_detail_head_classify_type" href="" target="_blank">财富</a></p>
    
    
    想用java 提取财富两个字 请问用正则怎么提取 

    用jsoup会不会简单一点
習慣沉默
習慣沉默

répondre à tous(2)
伊谢尔伦

Vous pouvez utiliser jsoup et regex, il est recommandé d'utiliser jsoup !
Document jsoup :
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 id=\"info_detail_head_classify_type\" href=\"\" 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));
            }
        }
    }
淡淡烟草味
<a[^>]*>([^<]*)</a>

Obtenez le contenu dans <a></a>

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!