Reputation: 15723
String s="<br /><br /><br /><br /><br />hello world!<br /><br /><br />";
s=s.replaceAll("<br /><br />", "<br />");
System.out.println(s);
//result: <br /><br /><br />hello world!<br /><br />
I want to get <br />hello world!<br />
how to do? thanks :)
Upvotes: 2
Views: 1416
Reputation: 155
Try this:
String healthyString = badString.replaceAll("(<br(.*?\\/?)>)+", "<br />");
or, according to your example...
s = s.replaceAll("(<br(.*?\\/?)>)+", "<br />");
This expression will solve the problem. It matches all line-break combinations:
<br>
, <br/>
, and <br />
.
Upvotes: 0
Reputation: 3523
Use s = s.replaceAll(/(<br\s*\/?>)+/, "<br />");
. This should replace any number of <br />
s by one.
Upvotes: 5