Getting multiple matches via regex

前端 未结 2 858
-上瘾入骨i
-上瘾入骨i 2021-01-21 22:07

I want to retrieve a strings from a global string via Matcher & Pattern using REGEX.

String str = \"ABC123DEF<         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-21 22:43

    I recommend to use JSOUP to parse your HTML code instead of regex as

        Document doc = Jsoup.parse("ABC123DEF");
    
        // select your tag
        Elements elements = doc.select("strong");
    
        // get the iterator to traverse all elements
        Iterator it =  elements.iterator();
    
        // loop through all elements and fetch their text
        while (it.hasNext()) {
            System.out.println(it.next().text());
        }
    

    Output :

    ABC
    DEF
    

    or get Output as single string

        Document doc = Jsoup.parse("ABC123DEF");
        Elements elements = doc.select("strong");
        System.out.println(elements.text());
    

    Output:

    ABC DEF
    

    Download Jsoup and add it as a dependency

提交回复
热议问题