# Help with regular expression

**URL:** <https://www.ruby-forum.com/t/help-with-regular-expression/78724>\
**Category:** Rails\
**Created:** [December 8, 2006, 2:00am UTC](https://www.ruby-forum.com/t/help-with-regular-expression/78724 "2006-12-08T02:00:58Z")\
**Posts on this page:** 5\
**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 8, 2006, 2:00am UTC](https://www.ruby-forum.com/t/help-with-regular-expression/78724/1 "2006-12-08T02:00:58Z")

</div>

Someone helped me earlier today with a method to remove the “pre” tag  
block and replace it with ‘’. This is because when I’m showing snippets  
of a post, I don’t want a piece of code to be in the snippet. Anyway,  
here’s what I’m doing:

# works perfect (only for pre)

def strip\_pre\_block  
text.gsub(/

```
[^<]*</pre>/,’’)
end

Now, I want to do the same with blockquote. How can I combine the two?  
Something like this:

# doesn’t work at all (for either one)

def strip\_pre\_and\_quote\_blocks  
text.gsub(/(^

```
[^<]_\</pre\>$|^_

_> ._

</blockquote>$)/,’’)
end

When I try that, it doesn’t replace either one of the “pre” or  
“blockquote” blocks with ‘’, but the first method works perfect for the  
“pre” block only. I can’t even get the blockquote one to work by  
itself.

Can someone help out with 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 8, 2006, 2:02am UTC](https://www.ruby-forum.com/t/help-with-regular-expression/78724/2 "2006-12-08T02:02:41Z")

</div>

I should note, the format of my blockquotes look like this:

> Quote goes here

I may have been ignoring the “p” tags so it didn’t work.

Still can’t get it. Any help is greatly appreciated.

---

<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 8, 2006, 4:07am UTC](https://www.ruby-forum.com/t/help-with-regular-expression/78724/3 "2006-12-08T04:07:43Z")

</div>

ryan wrote:

> # doesn’t work at all (for either one)
> 
> def strip\_pre\_and\_quote\_blocks  
> text.gsub(/(^
> 
> ```
> [^<]_\</pre\>$|^_
> 
> > _._\</blockquote\>$)/,‘’)  
> > end
> 
> Here are some general suggestions. Write a test case for this regexp,  
> have  
> it print out a result, and run it over and over again as you  
> incrementally  
> add elements. I note this because you appear to have leapt directly from  
> a  
> simple to a complex regexp without incrementally examining the reaction  
> to  
> each individual symbol. Get used to writing test cases as experiments,  
> and  
> frequently running them (with one keystroke preferrably).
> 
> Next, put the ^ and $ outside the (), on principle.
> 
> Next, try (
> 
> ```
> |
> 
> > ).
> > 
> > Next, [^\<]\* is always tempting, but you can get less greedy IIRC with  
> > .\*?.  
> > That will look-ahead a little more.
> > 
> > Next, post this to a Ruby or Regex newsgroup, because it’s not about  
> > Rails.
> > 
> > Next, treat your HTML as XHTML and parse it with REXML. You can use  
> > XPath to  
> > reach in and nab each pre, and change its tag or contents to whatever  
> > you  
> > want. Then write it all back as XHTML.
> > 
> > Next, each \< probably needs an escape, like \<
> > 
> > > When I try that, it doesn’t replace either one of the “pre” or  
> > > “blockquote” blocks with ‘’, but the first method works perfect for the  
> > > “pre” block only. I can’t even get the blockquote one to work by  
> > > itself.
> > 
> > You could run two gsubs; one with
> > 
> > ```
> > and one with
> > 
> > > .
> > > 
> > > To strip, you could just yank all 4 items with 4 gsubs, too.
> > > 
> > > –  
> > > Phlip  
> > > [Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!
> > ```
> ```
> ```

---

<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 8, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/78724/4 "2006-12-08T15:26:44Z")

</div>

On Dec 7, 2006, at 10:05 PM, Phlip wrote:

> incrementally  
> Next, try (
> 
> ```
> |
> 
> > ).  
> > reach in and nab each pre, and change its tag or contents to
> > 
> > You could run two gsubs; one with
> > 
> > ```
> > and one with
> > 
> > > .
> > > 
> > > To strip, you could just yank all 4 items with 4 gsubs, too.
> > > 
> > > –  
> > > Phlip  
> > > [Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!
> > 
> > If you’re trying to get rid of just the tags, then this might work.  
> > However, if you intend to remove the content between the tags, too  
> > (“The
> > 
> > ```
> > stuff doesn’t
> > ```
> > work” => “The work”), then you either
> > need to specifically match the newlines or just use the ‘m’ modifier
> > on your regexp to use multi-line mode.
> > 
> > If you want to remove
> > 
> > ```
> > and
> > 
> > > along with the contents  
> > > (inner\_html) of those, then you can try something like this:
> > > 
> > > irb\>\> regexp = %r{\<(pre|blockquote)\b[^\>]_\>._?\</\1\>}m  
> > > =\> /\<(pre|blockquote)\b[^\>]_\>._?\</\1\>/m
> > > 
> > > irb\>\> html\_fragment = \<\<EOF  
> > > And then someone said:
> > > 
> > > > When I try that, it doesn't replace either one of the "pre" or "blockquote" blocks with '', but the first method works perfect for the "pre" block only. I can't even get the blockquote one to work by itself.
> > > 
> > > So I told them to use this code:
> > > ```
> > > regexp = %r{<(pre|blockquote)\\b[^>]*>.*?}m
> > > my_string = <<-EOS
> > > And then someone said:
> > > 
> > > > When I try that, it doesn't replace either one of the "pre" or "blockquote" blocks with '', but the first method works perfect for the "pre" block only. I can't even get the blockquote one to work by itself.
> > > 
> > > EOS
> > > my_string.gsub!(regexp, '...')
> > > ```
> > > 
> > > > When I try that, it doesn't replace either one of the "pre" or "blockquote" blocks with '', but the first method works perfect for the "pre" block only. I can't even get the blockquote one to work by itself.
> > > 
> > > So I told them to use this code:
> > > ```
> > > regexp = %r{<(pre|blockquote)\\b[^>]*>.*?}m
> > > my_string = <<-EOS
> > > And then someone said:
> > > 
> > > > When I try that, it doesn't replace either one of the "pre" or "blockquote" blocks with '', but the first method works perfect for the "pre" block only. I can't even get the blockquote one to work by itself.
> > > 
> > > EOS
> > > my_string.gsub!(regexp, '...')
> > > ```
> > > EOF =\> "And then someone said:\n
> > > 
> > > > \nWhen I try that, it doesn't replace either one of the \"pre\" or\n\"blockquote\" blocks with '', but the first method works perfect for the\n\"pre\" block only. I can't even get the blockquote one to work by\nitself.\n
> > > 
> > > \nSo I told them to use this code:\n
> > > ```
> > > \n regexp = %
> > > r{<(pre|blockquote)\\b[^>]*>.*?}m\n my_string = <<-EOS\nAnd
> > > then someone said:\n
> > > 
> > > > \nWhen I try that, it doesn't replace either one of the \"pre\" or\n\"blockquote\" blocks with '', but the first method works perfect for the\n\"pre\" block only. I can't even get the blockquote one to work by\nitself.\n
> > > 
> > > \n EOS\n
> > > my_string.gsub!(regexp, '...')\n
> > > ```
> > > \n"
> > > 
> > > irb\>\> html\_fragment.gsub(regexp, ‘…’)  
> > > =\> “And then someone said:\n…\nSo I told them to use this code:\n…  
> > > \n”
> > > 
> > > irb\>\> puts \_  
> > > And then someone said:  
> > > …  
> > > So I told them to use this code:  
> > > …  
> > > =\> nil
> > > 
> > > irb\>\>
> > > 
> > > In addition to using an XML parser as Philip suggests, you might want  
> > > to actually read about regular expressions. You can start with the  
> > > pickaxe (Programming Ruby, The Pragmatic Programmers’ Guide, 2nd ed.)  
> > > pages 68-70, 324-328, 600-603.
> > > 
> > > -Rob
> > > 
> > > Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > > removed\_email\_address@domain.invalid
> > ```
> > ```
> ```

---

<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 8, 2006, 4:31pm UTC](https://www.ruby-forum.com/t/help-with-regular-expression/78724/5 "2006-12-08T16:31:00Z")

</div>

Ryan,

I agree with Phlip. This has crossed into territory better served by a  
real HTML parser. Something like this (untested):

require ‘hpricot’  
doc =  
Hpricot(“Keep

```
Toss
```
Keep

> Toss
> 
> Toss
> 
> Toss

”)

doc.search("//pre").each do |pre|  
pre.remove  
end

doc.search("//blockquote").each do |bq|  
bq.remove  
end

puts doc
