tags:

views:

297

answers:

4

Hi I have an html based text (with html tags), I want to find words that occur within angle brackets and replace the brackets with < and > or even when angle brackets are used as math symobls

e.g:

String text= "Hello, <b> Whatever <br /> <table> <tr> <td width="300px"> 
              1 < 2 This is a <test> </td> </tr> </table>";

I want this to be :

Hello,  <b> Whatever <br /> <table>  <tr> <td width="300px"> 
1 &lt; 2 This is a &lt; test &gt; </td> </tr> </table>

THANKS in advance

+3  A: 

I would suggest you to use Html Cleaner

If you look at the HomePage the example shows exactly how text is escaped.

<td><a href=index.html>1 -> Home Page</a>

is converted in

<td>
   <a href="index.html">1 -&gt; Home Page</a>
</td>

it will normalize your html to conform to standard xHtml. I used it in the past and (IMHO) it's pretty solid and more reliable than jTidy&Co. (and of course it's better then use regex or replace strategies...)

al nik
+1  A: 

Please see http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 and don't use regex to parse html. Use a SGML parser but don't use regex. It would fail to often. HTML isn't a regular language.

neo
A: 

If it were not for CSS, Javascript, and CData sections, it would be possible.

If you are only dealing with a subset of HTML, you could make the assumption that angle brackets not surrounded by valid element identifier characters can be encoded.

Something like "<(?=[^A-Za-z_:0-9/])" -> "<" and "(?<=[^A-Za-z_:0-9/])>" -> ">"

But, unless you are generating the HTML yourself and KNOW that it has no embedded CSS, javascript, CData, or object sections...

As fraido said, don't use regular expressions for non-regular languages.

Computer Linguist