Solved

java regular expressions - stripping html tags

Posted on 2007-11-18
6
3,350 Views
Last Modified: 2012-08-13
OK I am trying to strip off all html tags but this doesn't work...why not?
lines[i].replaceAll("\\<.*\\>", "");

Asusming I have a string called htmlPage, how do I convert the <p> and <br> to new lines? htmlPage is a string containing the whole html page and is multiline.

0
Comment
Question by:rukiman
6 Comments
 
LVL 23

Expert Comment

by:cmalakar
ID: 20310205
htmlString.replaceAll("<p>", "\n");

will replace all <p> tags into new lines...

Similary you can do for <BR> tag
0
 
LVL 23

Assisted Solution

by:cmalakar
cmalakar earned 60 total points
ID: 20310208
Also you can replace all tags by using..

htmlString = htmlString.replaceAll("<.*>", "");

dont forget, that replaceAll returns the resultant string..
0
 
LVL 92

Accepted Solution

by:
objects earned 65 total points
ID: 20310215
you're using a greedy quantifier, try:

lines[i].replaceAll("\\<.*?\\>", "");

> how do I convert the <p> and <br> to new lines?

line.replaceAll("\\<p\\>", "\n").replaceAll("\\<br\\>", "\n");
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 23

Expert Comment

by:cmalakar
ID: 20310265
Sorry...

typo mistake.

htmlString = htmlString.replaceAll("<.*>", "");

should be htmlString = htmlString.replaceAll("<[a-z]*>", "");
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20311268
what you look for is;
lines[i].replaceAll("</.*?>", "").replaceAll("<.*?>", "\n");
0
 

Author Comment

by:rukiman
ID: 20325330
I accepted cmalakar as a solution as I was completely unaware that replaceAll returned the resultant string.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
more than one jdk and one jre 1 50
Unhandled exception type Exception 18 42
swing controls 2 16
learn programming 8 39
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question