<p>Is there a good way to remove HTML from a Java string? A simple regex like</p>
<pre class="lang-js prettyprint-override"><code>replaceAll(&quot;\\&lt;.*?&gt;&quot;, &quot;&quot;) 
</code></pre>
<p>will work, but some things like <code>&amp;amp;</code> won't be converted correctly and non-HTML between the two angle brackets will be removed (i.e. the <code>.*?</code> in the regex will disappear).</p>

If the user enters
```
<b>hey!</b>
```
, do you want to display
```
<b>hey!</b>
```
or
```
hey!
```
?  If the first, escape less-thans, and html-encode ampersands (and optionally quotes) and you're fine.  A modification to your code to implement the second option would be:
```
```
replaceAll("\\<[^>]*>","")
```
```
but you will run into issues if the user enters something malformed, like
```
<bhey!</b>
```
.
You can also check out
JTidy
which will parse "dirty" html input, and should give you a way to remove the tags, keeping the text.
The problem with trying to strip html is that browsers have very lenient parsers, more lenient than any library you can find will, so even if you do your best to strip all tags (using the replace method above, a DOM library, or JTidy), you will
still
need to make sure to encode any remaining HTML special characters to keep your output safe.