I wanted a regular expression to parse an (x)HTML snibbet and escape the contents of pre tags, non-greedily.

Its most likely a multi-line string, so I wanted something with a wider scope than cat and sed. Perl? Probably, but Ruby’s object-oriented nature is so wonderful. I tried it, and as usual the regular expressions took a little bit of work, but this is what I came up with:

mystring = %q{<bodycontent>
<h2>Greetings!</h2>
<p>How are you?</p>
<pre>
hi
<
/pre>
</pre>
O
<pre>OK<br /></pre>
</bodycontent>
}
mystring.gsub!(/<pre>.*?<\/pre>/m) {|esc|
    esc.gsub('<br />',"\n").gsub('<','&lt;').gsub(/&lt;(\/)?pre>/,'<\1pre>')
}
puts mystring