Help with a regex

I’m trying to find a block of text within

 text 
tags, and
replace it with nothing. However, I’m not experienced with regular
expressions. Here’s what I want to do.

s = “Here are some words.

 this is a code snippet 
More
words.”
s.gsub([regex_goes_here],’’)

Now, s becomes:

$: s
=> “Here are some words. More words.”

I just don’t know the regular expression for this. Is it something
like…

s.gsub(/^[:

:][:
:]$, ‘’)

???

That doesn’t seem to work. Any help on this? Thanks in advance.

gsub(/

.*</pre>/, “
”)

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?

Bala P. 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?

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.

Thanks again for the help…

Ryan, here is the Ruby mailing list:

ruby-talk-google
[email protected]

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

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

So, maybe I’m doing something wrong, but this isn’t working:

gsub(/

.*</pre>/, “
”)

Here’s what I have:

#application_helper.rb
def strip_pre_block(text)
text.gsub(/

.*</pre>/, ‘’)
text
end

#view
<%= strip_pre_block(to_html(p.body)) %>

The to_html converts the textile text into html.

See anything wrong with this regex?

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.

Thank you - the regex you gave me worked like a charm.