# Removing a block of text within a string

**URL:** <https://www.ruby-forum.com/t/removing-a-block-of-text-within-a-string/78605>\
**Category:** Rails\
**Created:** [December 7, 2006, 7:13am UTC](https://www.ruby-forum.com/t/removing-a-block-of-text-within-a-string/78605 "2006-12-07T07:13:49Z")\
**Posts on this page:** 6\
**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, 7:13am UTC](https://www.ruby-forum.com/t/removing-a-block-of-text-within-a-string/78605/1 "2006-12-07T07:13:49Z")

</div>

When you first visit my site, you see a snippet (the first 75 words) of  
the most recent post. If it exceeds 75 words, a link will be appended  
to extend the post and read it in full.

Now, my problem is when I post a code snippet, and I use “pre” tags to  
preserve its formatting, I don’t want that to show up on the snippet…  
only on the full version of the post. The reason is because the site  
get’s all funky if the 75 work mark happens to fall in between the open  
“pre” and the closed “pre” and they are no properly matched.

I’m using textile for text formatting, so in my view I currently have  
something like this:

 \<%= to\_html(post.body) %\>

The “to\_html” helper simply converts the textile text into html. I  
would like to write a helper that would accept the converted html text  
as a parameter, and just completely remove the

```
snippet
```
  
block if I’m not on the full post. So, I’d like something like this:
 \<%= strip\_code\_snippets(to\_html(post.body)) %\>

Can someone help me with how to do this? What would the  
“strip\_code\_snippets” method look like? I think I’d be fine finding the

```
tag but I don't know how to completely remove the text from
```
to
```
. Any help would be greatly appreciated. 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, 7:47am UTC](https://www.ruby-forum.com/t/removing-a-block-of-text-within-a-string/78605/2 "2006-12-07T07:47:39Z")

</div>

my\_string = "

```
dflakdjflakj
```
  
my\_string.gsub(/[^
```
][
```
$], ‘’)

You basically create a regular expression to find the

```
and
```
  
and nuke all the characters when you find the match.

I dont think my regex is correct. Advanced Ruby developers here can  
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, 2:37pm UTC](https://www.ruby-forum.com/t/removing-a-block-of-text-within-a-string/78605/3 "2006-12-07T14:37:17Z")

</div>

Ok, that’s what I thought it would be. I’m not very good at regular  
expressions, as I’ve had little experience with them. I’ll try it out,  
though. Thanks a lot. Anyone else know if this regex will work? Or  
have a better one? Thanks.

---

<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:53pm UTC](https://www.ruby-forum.com/t/removing-a-block-of-text-within-a-string/78605/4 "2006-12-07T16:53:23Z")

</div>

On Dec 7, 2006, at 1:45 AM, Bala P. wrote:

> You basically create a regular expression to find the
> 
> ```
> and
> ```
>   
> and nuke all the characters when you find the match.
> 
> I dont think my regex is correct. Advanced Ruby developers here can  
> help.

> > my\_string = “This is my little block of code
> > 
> > ```
> > puts ‘I need to
> > learn about Regexp’\nputs ‘Will you help?’
> > ```
> > ”  
> > =\> “This is my little block of code 
> > ```
> > puts ‘I need to learn about
> > Regexp’\nputs ‘Will you help?’
> > ```
> > ”  
> > regexp = %r{\<pre\b[^\>]_\>._?}m  
> > =\> /\<pre\b[^\>]_\>._?\</pre\>/m  
> > my\_string.gsub(regexp, ‘’)  
> > =\> "This is my little block of code "

The square brackets […] enclose a character set and [^…] enclose  
a negated set (not one of those characters. The previous posting  
isn’t even well-formed syntactically.

Here’s a little explanation to get you started:

regexp = %r{\<pre\b[^\>]_\>._?}m

```
] = character set matching anything that's NOT a >
* = zero or more times
 > = literal >
.*? = any character (.) repeated zero or more times, but as few as
possible to let the regexp match (*?)
        Note: this is getting rather advanced, you can look at the
pickaxe pp.68-77
```
 = literal character matching

The %r{ } is an alternate way to write a literal regular expression  
which I used in lieu of escaping the / in the /pre. You can see the  
equivalent form that irb printed as the value.

The ‘m’ at the end is a flag to match multi-line input. It turns the  
‘.’ from matching “any character except newline” to simply “any  
character”.

-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 7, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/removing-a-block-of-text-within-a-string/78605/5 "2006-12-07T17:57:52Z")

</div>

I haven’t seen this come back from the list so I thought a copy  
directly to you might be useful.

-Rob

Begin forwarded message:

---

<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, 10:57pm UTC](https://www.ruby-forum.com/t/removing-a-block-of-text-within-a-string/78605/6 "2006-12-07T22:57:14Z")

</div>

Sorry if this gets dup’d, I haven’t seen it hit the list after 8hrs.

On Dec 7, 2006, at 1:45 AM, Bala P. wrote:

> You basically create a regular expression to find the
> 
> ```
> and
> ```
>   
> and nuke all the characters when you find the match.
> 
> I dont think my regex is correct. Advanced Ruby developers here can  
> help.

> > my\_string = “This is my little block of code
> > 
> > ```
> > puts ‘I need to
> > learn about Regexp’\nputs ‘Will you help?’
> > ```
> > ”  
> > =\> “This is my little block of code 
> > ```
> > puts ‘I need to learn about
> > Regexp’\nputs ‘Will you help?’
> > ```
> > ”  
> > regexp = %r{\<pre\b[^\>]_\>._?}m  
> > =\> /\<pre\b[^\>]_\>._?\</pre\>/m  
> > my\_string.gsub(regexp, ‘’)  
> > =\> "This is my little block of code "

The square brackets […] enclose a character set and [^…] enclose  
a negated set (not one of those characters. The previous posting  
isn’t even well-formed syntactically.

Here’s a little explanation to get you started:

regexp = %r{\<pre\b[^\>]_\>._?}m

```
] = character set matching anything that's NOT a >
* = zero or more times
 > = literal >
.*? = any character (.) repeated zero or more times, but as few as
possible to let the regexp match (*?)
        Note: this is getting rather advanced, you can look at the
pickaxe pp.68-77
```
 = literal character matching

The %r{ } is an alternate way to write a literal regular expression  
which I used in lieu of escaping the / in the /pre. You can see the  
equivalent form that irb printed as the value.

The ‘m’ at the end is a flag to match multi-line input. It turns the  
‘.’ from matching “any character except newline” to simply “any  
character”.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
