# Noob Question - String Manipulation

**URL:** <https://www.ruby-forum.com/t/noob-question-string-manipulation/59718>\
**Category:** Ruby\
**Created:** [May 5, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/noob-question-string-manipulation/59718 "2006-05-05T19:27:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 5, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/noob-question-string-manipulation/59718/1 "2006-05-05T19:27:58Z")

</div>

Hey, I’m pretty new to Ruby and am trying to absorb the “ruby way” of  
things as much as I can. What’s the best way to solve this problem:

I have a string that contains html formating. All I want is the plain  
text.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 5, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/noob-question-string-manipulation/59718/2 "2006-05-05T19:48:30Z")

</div>

On 5/5/06, Joe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey, I’m pretty new to Ruby and am trying to absorb the “ruby way” of  
> things as much as I can. What’s the best way to solve this problem:
> 
> I have a string that contains html formating. All I want is the plain  
> text.

I don’t know of any libraries offhand that can do this (CGI only has  
escape/unescape), but it’s fairly simple:

html\_string.gsub(/\<[^\>]+\>/, “”)

Replacing that regex with something better, probably.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/noob-question-string-manipulation/59718/3 "2006-05-05T19:57:48Z")

</div>

I’ve been using

html\_string.gsub(/\<.\*?\>/,"")

for this. But it’s always seemed more a “perl way” than a “ruby way”.

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 5, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/noob-question-string-manipulation/59718/4 "2006-05-05T20:15:10Z")

</div>

Joseph Michaels wrote:

> > I have a string that contains html formating. All I want is the plain  
> > text.  
> > html\_string.gsub(/\<[^\>]+\>/, “”)  
> > Replacing that regex with something better, probably.

gsubbing breaks down for more complex test cases, such as things  
containing source code, or problematic attribute strings (e.g. ).

If you really want to be accurate, I suggest using an XML or HTML  
parsing tool, such as Mechanize or RubyfulSoup.

Pistos

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [May 5, 2006, 8:50pm UTC](https://www.ruby-forum.com/t/noob-question-string-manipulation/59718/5 "2006-05-05T20:50:25Z")

</div>

On Sat, 2006-05-06 at 02:28 +0900, Joe C. wrote:

> Hey, I’m pretty new to Ruby and am trying to absorb the “ruby way” of  
> things as much as I can. What’s the best way to solve this problem:
> 
> I have a string that contains html formating. All I want is the plain  
> text.

There’s more to this than meets the eye - it’s often best to hand off  
the hard stuff to someone else 🙂

def fetchtext(uri)  
`lynx --dump #{uri}`  
end

puts fetchtext(‘[www.google.com](http://www.google.com)’)

# =\>

# [1]Personalised Home | [2]Sign in

# 

# Google

# 

# Web [3]Images [4]Groups [5]News [6]Froogle [7]more Â»

# 

# … [snipped] …
