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?
That’s exactly what I was thinking. I have padding in the CSS on my
“pre” tags, so if there isn’t any text there, I don’t want a block
element showing.
But that wasn’t the part I asked about - and it’s easy enough to just
switch it out and see if that works. The hard part was the regex part,
which I really appreciate. Regular expressions are always so easy once
I see it, but can be complicated when I’m trying to write one.
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.
I don’t think anyone mentioned that the regex will not work if you have
more than one
block in the text. In other words, it will fail in
this case:
str = “
DeleteMe
SaveMe
DeleteMe
”
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
tags, you can do the following:
def strip_pre_block(text)
text.gsub(/
[^<]*</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.