简体   繁体   中英

Java How to select half part of string?

I have string like this:

<html>
<body>
<p>Hello</p>
</body>
</html>

and In java I want to after selection string looked like this:

<p>Hello</p>

How?

If you are dealing with HTML parsing then go for JSoup

String html = "<html><body><p>Hello</p></body></html>";
Document doc = Jsoup.parseBodyFragment(html);
Elements fragment = doc.select("p"); // p tag
System.out.println(fragment.html());

This one line should do it:

String text = input.replaceAll("(?s).*(<p>.*</p>).*", "$1");

Or to just get everything in the <body> tag, do this:

String text = input.replaceAll("(?s).*<body>(.*)</body>.*", "$1");

您可以使用JSoup ,它将对您很方便。

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