# Help with a regex

**URL:** <https://www.ruby-forum.com/t/help-with-a-regex/78647>\
**Category:** Rails\
**Created:** [December 7, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647 "2006-12-07T16:07:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/1 "2006-12-07T16:07:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/2 "2006-12-07T16:21:13Z")

</div>

gsub(/

```
.*</pre>/, “
```

```
”)
```

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/3 "2006-12-07T16:41:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/4 "2006-12-07T17:05:09Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/5 "2006-12-07T17:45:49Z")

</div>

Ryan, here is the Ruby mailing list:

ruby-talk-google  
removed\_email\_address@domain.invalid

[http://groups.google.com/group/ruby-talk-google](http://groups.google.com/group/ruby-talk-google)

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

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/6 "2006-12-07T19:24:35Z")

</div>

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?
```
```

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/7 "2006-12-07T20:14:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/8 "2006-12-07T21:06:50Z")

</div>

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.
```
```

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [December 7, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/help-with-a-regex/78647/9 "2006-12-07T21:19:29Z")

</div>

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