简体   繁体   中英

cant remove all occurrence of html tag in java using pattern matching

I have very long html string which has multiple

             <dl id="divmap"> .... </dl>.

I want to remove all content between this .

i wrote this code in java:

                                   String triphtml= htmlString;
                System.out.println("triphtml is "+triphtml);

                System.out.println("test1 ");
                final Pattern pattern = Pattern.compile("(<dl id=\""+selectedArray[i]+"\">)(.+?)(</dl>)",
                        Pattern.DOTALL);
                final Matcher matcher = pattern.matcher(triphtml);
                // matcher.find();
                System.out.println("pattern of test1 is : "
                        + pattern); // Prints
                System.out.println("MATCHER of test1 is : "
                        + matcher); // Prints
                System.out.println("MATCH COUNT of test1 a: "
                        + matcher.groupCount()); // Prints
                System.out.println("MATCH COUNT of test1  a: "
                        + matcher.find()); // Prints
                while (matcher.find()) {
                    // System.out.println("MATCH GP 3: "+matcher.group(3).substring(1,10));

                    for (int z = 0; z <= matcher.groupCount(); z++) {
                        String extstr = matcher.group(z);
                        System.out.println("matcher group of "+z+" test1  is " + extstr);
                        System.out.println("ext a of test1  is " + extstr);
                        triphtml = triphtml.replaceAll(extstr, "");
                        System.out.println("Group found of test1 is :\n" + extstr);
                    }

                }

But this code removes some dl and some remains in triphtml. I dont why this thing is happening. Here triphtml is a html string which has multiple dl's. Please help me how I remove content between all

    <dl id="divmap">.

Thanks in advance.

I suggest to NOT use regex for html. Just use any library used for traversing xml/html.

For example JSoup

By using regex you can do as follows:

String orgString = "<dl id=\"divmap\"> .... </dl>";

orgString = orgString.replaceAll("<[^>]*>", "");
//for removing html tag

orgString = orgString.replaceAll(orgString.replaceAll("<[^>]*>", ""),"");
//for removing content inside html tag

But it is better to use html parsing

Edit :

String htmlString = "<dl id=\"divmap\"> Content </dl>";
Pattern p = Pattern.compile("<[^>]*>");
Matcher m = p.matcher(htmlString);
while(m.find()){
    htmlString = htmlString.replaceAll(m.group(), "");
}
System.out.println("Ans"+htmlString);

Try using JSoup

It uses selectors and syntax like JQuery, it it very easy to use.

You can try this

String triphtml = htmlString;

Document doc = Jsoup.parse(htmlString);
Elements divmaps = doc.select("#divmap");

then you can remove (or alter) the elements in the DOM.

divmaps.remove();
triphtml = doc.html();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM