# What's the best way of parsing this string?

**URL:** <https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351>\
**Category:** Ruby\
**Created:** [February 12, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351 "2007-02-12T22:36:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 12, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/1 "2007-02-12T22:36:19Z")

</div>

Hi there,

I have a question about string search and replacement. Let’s say I have  
this  
string that contains 2 links for embedding youtube videos  
amongst  
some other random text.

string = “We’ve got some junk text here” +  
“\<object width="425" height="350"\>\<param name="movie"  
value="[- YouTube](http://www.youtube.com/v/qqsyXdj_p_I%5C%22%3E%3C/param%3E%22) +  
“\<param name="wmode" value="transparent"\>\<embed  
src="  
[- YouTube](http://www.youtube.com/v/qqsyXdj_p_I%5C)”  
type="application/x-shockwave-flash"” +  
“wmode="transparent" width="425"  
height="350"\>” +  
“And we’ve got some more junk text right here” +  
“\<object width="425" height="350"\>\<param name="movie"  
value="[- YouTube](http://www.youtube.com/v/yYqACgndOQA%5C%22%3E%3C/param%3E%22) +  
“\<param name="wmode" value="transparent"\>\<embed  
src="  
[- YouTube](http://www.youtube.com/v/yYqACgndOQA%5C)”  
type="application/x-shockwave-flash"” +  
“wmode="transparent" width="425"  
height="350"\>” +  
“more garbage text here”

I’d like to get the value of the ‘value’ attributes ("  
[- YouTube](http://www.youtube.com/v/qqsyXdj_p_I)" and "  
[- YouTube](http://www.youtube.com/v/yYqACgndOQA)" respectively) and convert the  
string  
into the following:

We’ve got some junk text here

And we've got some more junk text right here

more garbage text here

What would be the best library to use for parsing and replacing certain  
values in a string? I’ve done simple .gsub’ing before, but this seems to  
be  
a little more complicated 😉

Thanks,  
Dave H.

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 12, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/2 "2007-02-12T22:50:20Z")

</div>

Well, maybe you can assume valid XML and parse the page with REXML. If  
it’s  
not valid XML, well you can regex through for (off-the-cuff, probably  
wrong)  
/\<param._value="(._)"/ and use the result. Someone more knowledgable in  
Regexps can help you out if it comes to this.

Jason

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 12, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/3 "2007-02-12T23:09:23Z")

</div>

Hi Jason,

Thanks for the suggestion. Why would REXML be a good use though?

-Dave

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 13, 2007, 12:58am UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/4 "2007-02-13T00:58:38Z")

</div>

Thanks Mark… I’ll look in to that.

-Dave

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 12, 2007, 11:46pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/5 "2007-02-12T23:46:27Z")

</div>

Hi Dave,  
REXML gets you into the structure of xhtml (compliant) docs.  
There is a learn.  
There is a different way of things.

The Tutorial page is a lesson in itself.  
An astonishing compression of answers in one page.  
[http://www.germane-software.com/software/XML/rexml/docs/tutorial.html](http://www.germane-software.com/software/XML/rexml/docs/tutorial.html)

Markt

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 13, 2007, 2:17am UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/6 "2007-02-13T02:17:45Z")

</div>

On 2/12/07, Dave H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a question about string search and replacement. Let’s say I have this  
> string that contains 2 links for embedding youtube videos amongst  
> some other random text.

I haven’t used it yet, but I hear really good things about Hpricot.

-austin

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 13, 2007, 4:01am UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/7 "2007-02-13T04:01:02Z")

</div>

Hi,

On Tuesday 13 February 2007 02:16, Austin Z. wrote:

> On 2/12/07, Dave H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have a question about string search and replacement. Let’s say I have  
> > this string that contains 2 links for embedding youtube videos  
> > amongst some other random text.
> 
> I haven’t used it yet, but I hear really good things about Hpricot.

i’ve used it[1], i needed to pull out some statistical data off a bunch  
of  
html pages slightly different from one to another, combined with  
firebug[2]'s  
ability to generate xpath expression by simply pointing at an  
element[3], and  
recent hpricot’s support for xpath indices… it should be a matter of  
minutes  
of automatically extracting anything you want from any html page. 🙂

[1] [http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/)  
[2] [http://www.getfirebug.com/](http://www.getfirebug.com/)  
[3] version 1.0 is simply _awesome_

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 13, 2007, 2:48pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/8 "2007-02-13T14:48:59Z")

</div>

On 2/12/07, Jason R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Well, maybe you can assume valid XML and parse the page with REXML. If  
> it’s  
> not valid XML, well you can regex through for (off-the-cuff, probably  
> wrong)  
> /\<param._value="(._)"/ and use the result. Someone more knowledgable in  
> Regexps can help you out if it comes to this.

Well it might be necessary to use a non greedy match

/\<param._value="(._?)"/

in order not to consume a potentially following key=“…” pair.

A more explicit and thus more readable way might be to write it like  
this -  
avoiding any potential backtracking issues if the regexp evolves later  
too.

/\<param._value=“([^”]_)"/

This all is just for the quick hack though, definitely go with REXML or  
hpricot if they can do the job for you.

HTH  
Roberts

Jason

---

<div class="post-metadata">

**Author:** ![daveh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/85e7bf/32.png) [@daveh](https://www.ruby-forum.com/u/daveh)\
**Post date:** [February 13, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-of-parsing-this-string/85351/9 "2007-02-13T16:42:13Z")

</div>

Robert D. wrote:  
[…]

> Well it might be necessary to use a non greedy match
> 
> /\<param._value="(._?)"/
> 
> in order not to consume a potentially following key="…" pair.
> 
> A more explicit and thus more readable way might be to write it like this -  
> avoiding any potential backtracking issues if the regexp evolves later too.
> 
> /\<param._value="([^"]_)"/

Your advice of using a non greedy match is good, but the example using a  
greedy match is not 😉

your\_re =~ ’ … bla bla bla … value=“ha!”’  
puts $1

Greetings.
