Alex Rivera | Logout

Regex to strip HTML tags

Asked 2010-11-02T07:42:35.420
18

I have this HTML input:

<font size="5"><p>some text</p>
<p> another text</p></font>

I'd like to use regex to remove the HTML tags so that the output is:

some text
another text

Can anyone suggest how to do this with regex?

Edit
Report

1 Answer

2

If you use Jericho, then you just have to use something like this:

public String extractAllText(String htmlText){
    Source source = new Source(htmlText);
    return source.getTextExtractor().toString();
}

Of course you can do the same even with an Element:

for (Element link : links) {
  System.out.println(link.getTextExtractor().toString());
}
answered 2011-01-02T15:48:39.577

Your Answer