in reply to substitution on

< and > are special characters for html. Your text is nearly unreadable, please edit your node and use &lt; or code-tags to print those characters.

To answer your question, you might use something like this:

s/<(?=[^>]*(<|$))/&lt;/g;

This substitutes all < not followed by > but instead by < or end of the string

Naturally this won't work if single < could occur between a valid < ... > construct. It also won't work if a > might be on the next line to its opening <. In that case you could slurp in the whole file into one string.