# Converting the string

**URL:** <https://www.ruby-forum.com/t/converting-the-string/171396>\
**Category:** Ruby\
**Created:** [July 1, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/converting-the-string/171396 "2009-07-01T18:25:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neverhood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@neverhood](https://www.ruby-forum.com/u/neverhood)\
**Post date:** [July 1, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/converting-the-string/171396/1 "2009-07-01T18:25:05Z")

</div>

Hi everybody! I`m sorry for asking a silly question .. i hope you`ll  
find some time to assist

while searching shrough html body i get the string :

 ![](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF)

how can i modify it to get just a plain link without tags and params? :  
[https://sc.omniture.com/sc13\_5/reports/chart.php?id=CPRIY\_6NvJ\_kj7s&s=www461.sj2&type=GIF](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF)

---

<div class="post-metadata">

**Author:** ![neverhood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@neverhood](https://www.ruby-forum.com/u/neverhood)\
**Post date:** [July 1, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/converting-the-string/171396/2 "2009-07-01T19:22:15Z")

</div>

Vlad Smith wrote:

> Hi everybody! I`m sorry for asking a silly question .. i hope you`ll  
> find some time to assist
> 
> while searching shrough html body i get the string :
> 
> ![](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF)
> 
> how can i modify it to get just a plain link without tags and params? :  
> [https://sc.omniture.com/sc13\_5/reports/chart.php?id=CPRIY\_6NvJ\_kj7s&s=www461.sj2&type=GIF](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF)

You’ll have to expand this to take care of all possible scenarios, but  
here’s an example:

x = “\<img src="http://some\_url\_to\_scrape"\>”  
y = x.scan(/src="([\S\s]+?)"/)

That will return an array, and you’ll have to fish the string out of the  
array with y[0][0].

That works specifically with proper HTML using double quotes where your  
examples above used malformed single quotes, but you can either use  
multple expressions, or build a more complex one to cover the various  
cases of quotes href, src, and other attributes names, etc.

There’s other ways to do it, this is just a small example to give you  
some ideas.

– gw

---

<div class="post-metadata">

**Author:** ![neverhood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@neverhood](https://www.ruby-forum.com/u/neverhood)\
**Post date:** [July 1, 2009, 11:21pm UTC](https://www.ruby-forum.com/t/converting-the-string/171396/3 "2009-07-01T23:21:10Z")

</div>

On Thu, Jul 02, 2009 at 05:29:04AM +0900, Vlad Smith wrote:

> > > 
> > 
> > array with y[0][0].
> 
> Thanks! that worked!
> 
> i also accidently noticed a great feature taken from perl that worked  
> also:
> 
> x = \<img  
> src='[https://sc.omniture.com/sc13\_5/reports/chart.php?id=CPRIY\_6NvJ\_kj7s](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s)  
> s=www461.sj2&type=GIF’width=624 height=280 border=0  
> align=absmiddleusemap=#imIY\_6NvJ\_kj7s\>  
> x = $1 if x =~ /.\*(https._GIF)._/

Please don’t do this. Every time you parse HTML with a regular  
expression, a kitten dies.

Instead, try using an HTML parsing library:

x = \<\<-eohtml

 ![](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7ss=www461.sj2&type=GIF)eohtml

puts Nokogiri::HTML(x).at(‘img’)[‘src’]

---

<div class="post-metadata">

**Author:** ![neverhood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@neverhood](https://www.ruby-forum.com/u/neverhood)\
**Post date:** [July 1, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/converting-the-string/171396/4 "2009-07-01T22:29:01Z")

</div>

Greg W. wrote:

> Vlad Smith wrote:
> 
> > Hi everybody! I`m sorry for asking a silly question .. i hope you`ll  
> > find some time to assist
> > 
> > while searching shrough html body i get the string :
> > 
> > ![](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF)
> > 
> > how can i modify it to get just a plain link without tags and params? :  
> > [https://sc.omniture.com/sc13\_5/reports/chart.php?id=CPRIY\_6NvJ\_kj7s&s=www461.sj2&type=GIF](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s&s=www461.sj2&type=GIF)
> 
> You’ll have to expand this to take care of all possible scenarios, but  
> here’s an example:
> 
> x = “\<img src="http://some\_url\_to\_scrape"\>”  
> y = x.scan(/src="([\S\s]+?)"/)
> 
> That will return an array, and you’ll have to fish the string out of the  
> array with y[0][0].
> 
> That works specifically with proper HTML using double quotes where your  
> examples above used malformed single quotes, but you can either use  
> multple expressions, or build a more complex one to cover the various  
> cases of quotes href, src, and other attributes names, etc.
> 
> There’s other ways to do it, this is just a small example to give you  
> some ideas.
> 
> – gw

Thanks! that worked!

i also accidently noticed a great feature taken from perl that worked  
also:

x = \<img  
src='[https://sc.omniture.com/sc13\_5/reports/chart.php?id=CPRIY\_6NvJ\_kj7s](https://sc.omniture.com/sc13_5/reports/chart.php?id=CPRIY_6NvJ_kj7s)  
s=www461.sj2&type=GIF’width=624 height=280 border=0  
align=absmiddleusemap=#imIY\_6NvJ\_kj7s\>  
x = $1 if x =~ /.\*(https._GIF)._/
