# Help on extracting data from string

**URL:** <https://www.ruby-forum.com/t/help-on-extracting-data-from-string/92835>\
**Category:** Rails\
**Created:** [April 24, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/help-on-extracting-data-from-string/92835 "2007-04-24T20:32:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 24, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/help-on-extracting-data-from-string/92835/1 "2007-04-24T20:32:28Z")

</div>

Hi,

Has anyone got any suggestions on the bes way to solve perform this, I  
have tried using HTMLTokenizer but had no luck.

I have a string which will have an unlimited amounts of

```
ruby
stuff here
```

 in it, I want to be able to get the text inside  
these two tags and run formatt function on them.

Any suggestions on the best way to do this?

thanks  
josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 24, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/help-on-extracting-data-from-string/92835/2 "2007-04-24T21:18:14Z")

</div>

josh wrote:

> I have a string which will have an unlimited amounts of
> 
> ```
> ruby
> stuff here
> ```
> 
> in it, I want to be able to get the text inside  
> these two tags and run formatt function on them.
> 
> Any suggestions on the best way to do this?
> 
> thanks  
> josh

How about using the sub/gsub method?

string.sub(Regexp.escape(

```
), ‘’).gsub
%r(#{Regexp.escape(
```

)}$), ‘’

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 24, 2007, 10:13pm UTC](https://www.ruby-forum.com/t/help-on-extracting-data-from-string/92835/3 "2007-04-24T22:13:19Z")

</div>

josh wrote:

> I want to be able to get the text inside  
> these two tags and run formatt function on them.  
> maybe you want this:  
> [http://atomgiant.com/2006/08/01/coderay-syntax-highlighting-for-typo/](http://atomgiant.com/2006/08/01/coderay-syntax-highlighting-for-typo/)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 25, 2007, 12:10am UTC](https://www.ruby-forum.com/t/help-on-extracting-data-from-string/92835/4 "2007-04-25T00:10:41Z")

</div>

I’ve used Hpricot successfully for parsing HTML. It’s a quality gem.

[http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/)
