Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove div tag from string

Tags:

java

string

I have a string as follows:

This is a sample <b>String</b> I want to <div style="font-size:22px"> replace it fast </div>

How can I remove <div> start and </div> end tag from the above string in Java in one go?

like image 384
Hunt Avatar asked Jul 18 '26 09:07

Hunt


2 Answers

You can do this:

String s = "This is a sample <b>String</b> I want to <div style=\"font-size:22px\"> replace it fast </div>";
s = s.replaceAll("[<](/)?div[^>]*[>]", "");

This will result in:

This is a sample <b>String</b> I want to  replace it fast 
like image 123
wjans Avatar answered Jul 20 '26 22:07

wjans


Try the following:

myString.replaceAll("<div .*>(.*)</div>","$1");

btw: there is a online regular expression tester for java:

http://www.regexplanet.com/simple/index.html

(if you did not know it)

Hope that helps

like image 26
Chris Avatar answered Jul 21 '26 00:07

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!