Help with a regex

gsub(/<pre>.*<\/pre>/, "<pre></pre>")

ryan wrote:

Shouldn’t the second argument be single quote followed by another single quote to remove the text between the tags as well as the tags?

Ryan, here is the Ruby mailing list:

ruby-talk-google
ruby-talk@ruby-lang.org


http://groups.google.com/group/ruby-talk-google

For Ruby related questions, you will find this group very helpful.

yes. You're gsubbing the text, but not doing anything with it. Then you're returning your original text. Take that last statement out of your method, then it will return the results of the gsub.

ryan wrote:

I don't think anyone mentioned that the regex will not work if you have
more than one <pre> block in the text. In other words, it will fail in
this case:

  str = "<pre>DeleteMe</pre>SaveMe<pre>DeleteMe</pre>"

Because the .* is greedy, it'll get rid of the entire string, including
the SaveMe part. If you KNOW there won't be any HTML tags inbetween the
<pre></pre> tags, you can do the following:

def strip_pre_block(text)
  text.gsub(/<pre>[^<]*<\/pre>/,'')
end

HTML is difficult to parse correctly using Regular Expressions.
Ordinarily I would recommend using a real HTML parser, but in your
case, you may be able to use the amended regex above, or even process
the textile before you convert it to HTML.