3

I want to remove the word "<br>" if it occures more than once consecutively. example

"word word <br><br>" becomes "word word <br>"

and "word <br><br><br> word <br><br>" becomes "word <br> word<br>"

I want to use replace or replace all if thats possible to keep it short

124697
  • 22,097
  • 68
  • 188
  • 315
  • @user521180: question ain't exactly your question but... http://stackoverflow.com/questions/1732348 You do really want to read the first answer :) – SyntaxT3rr0r Mar 03 '11 at 19:31
  • @Syntax but the first answer is a rediculously unreadable tirade on the evils of regular expressions who's link is posted on every question that has markup in it. I guess yours is one more. –  Mar 03 '11 at 20:35

3 Answers3

6

I want to use replace or replace all if thats possible to keep it short

Sure, it's possible:

yourString = yourString.replaceAll("(<br>)+", "<br>");

It basically means replace all "<br> one or more times" with just "<br>".

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Does it also capture the group? – adarshr Mar 03 '11 at 17:49
  • @adarshr, what do you mean? If you're asking if the group can be used in the replacement string, the answer is yes, through `$1`. – aioobe Mar 03 '11 at 17:51
  • 1
    @adarshr - yes, it captures a group for the replace. Poor cycles. If you're looking for efficiency you can write `(?:
    ){2,}`, but you may want to thing whether it's it's worth it.
    – Kobi Mar 03 '11 at 17:52
  • Yes, that's precisely what I meant! :) – adarshr Mar 03 '11 at 17:53
1

This code is good to simplest situation:

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

But if you want replace all br (case insensitive + ignore whitespaces), e.g.:

my word <BR>  <BR> blah blah -> my word <br> blah blah

I recommend you:

str.replaceAll("(?i)(<br>(\\s)*)+", "<br>")
lukastymo
  • 26,145
  • 14
  • 53
  • 66
0

You can write a regex that matches the pattern only if it occurs more than one time.

Jeremy Whitlock
  • 3,808
  • 26
  • 16