This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Need help - To replace <br> with Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Need help - To replace <br> with "" empty string" Watch "Need help - To replace <br> with "" empty string" New topic
Author

Need help - To replace <br> with "" empty string

nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Hello all,

I have an html string which contains br with beginning and ending <> brackets which I want to replace with the "" empty string. I am using the String.replaceAll() method eg message.replaceAll("[<(.|)+?>]", " "). I am able to delete the < and > but not the substring "br". Please help me to modify the regex/pattern.

I appreciate your time.

Thank you so much,
Naveen Katoch
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3036
    
    4

Any reason you don't do string.replaceAll("<br>", "");? I am not at a Java compiler so can't tell, but a quick regex test seemed to show it would work...


Steve
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Hi Steve,

Thanks for your time. My string is like this
String message = "<strong>" + title + "</strong>" +
"\n" +
"" + message.replaceAll("[<(.|)+?>]", " ") + "";
or message = title + "\n" + message.replaceAll("[<(.|)+?>]", " ");
_messages.add(message);

It is returnning the message with br as it has a substring like
. The returned message after replaceAll is Declined from Product Dispensing by Naveen Katoch br Edit Data Entry br. So I don't want the br in the returned message. Or is it possible to put br and replace with empty string. I am doing the same thing eg final String br = "br"; message = message.replaceAll(br, ""); but I wanted to create a pattern/regex inside replaceAll to replace
with an empty string.

Thanks again for your time,

Naveen Katoch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Why not use message.replace("<br>", "")?


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Hi Rob,

Thanks for your time. I fixed it by doing like this message.replaceAll("[<(.|)+?>](
)", " ").

Naveen
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56204
    
  13

Why have you not answered the question about using the straight-forward approach?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Need help - To replace <br> with "" empty string
 
Similar Threads
jsp form loading issue
MultiDimenaional Array sort
How to dynamically "Check" the radio button based on condition
Can not Populate the form on combo box change
passing hidden forms problem