Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert strings like "<br><br><br><br><br><br>" to "<br>" using java's String.replaceAll(String, String) method?

Tags:

java

string

regex

how to convert string

"<br><br><br><br><br><br>" to 
"<br>" using java's String.replaceAll(String, String) method?

I've tried both:

    str.replaceAll("<br>+","<br>");
    str.replaceAll("<br>{1,}","<br>);

but neither work.

like image 636
DaedalusUsedPerl Avatar asked Dec 28 '22 00:12

DaedalusUsedPerl


1 Answers

<br>+ matches "<br>>>>>>>>", try this instead: (<br>)+

And if there are spaces in between the tags, do:

str = str.replaceAll("(<br>\\s*)+","<br>");
like image 105
Bart Kiers Avatar answered Dec 30 '22 10:12

Bart Kiers