# Regexp help

**URL:** <https://www.ruby-forum.com/t/regexp-help/146465>\
**Category:** Ruby\
**Created:** [August 21, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/regexp-help/146465 "2008-08-21T12:48:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/regexp-help/146465/1 "2008-08-21T12:48:26Z")

</div>

I Need to Extract Img tag Using Regular Expressions From The Html Page  
\<\s_img [^\>]src\s=\s_(["’])(.\*?)\1  
Is This Code Would be ok

Can Any One Say Me Some Other regexp For Img Tag Extracing?

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/regexp-help/146465/2 "2008-08-21T12:54:39Z")

</div>

Newb N. wrote:

> I Need to Extract Img tag Using Regular Expressions From The Html Page  
> \<\s_img [^\>]src\s=\s_(["’])(.\*?)\1  
> Is This Code Would be ok
> 
> Can Any One Say Me Some Other regexp For Img Tag Extracing?

Instead of using a regular expression you could consider a html parser ,  
and/or do a xpath search to retrieve images. Check hpricot .

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 1:01pm UTC](https://www.ruby-forum.com/t/regexp-help/146465/3 "2008-08-21T13:01:07Z")

</div>

On Thu, Aug 21, 2008 at 12:50 PM, Lex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Instead of using a regular expression you could consider a html parser ,  
> and/or do a xpath search to retrieve images. Check hpricot .

Yeah, it is quite easy with Hpricot:

require ‘open-uri’  
require ‘hpricot’

site =  
Hpricot(open(“[http://code.google.com/edu/submissions/SedgewickWayne/index.html](http://code.google.com/edu/submissions/SedgewickWayne/index.html)”))  
site.search(“//img”) #=\> returns an array of all images

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/regexp-help/146465/4 "2008-08-21T13:41:14Z")

</div>

Thomas W. wrote:

> On Thu, Aug 21, 2008 at 12:50 PM, Lex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Instead of using a regular expression you could consider a html parser ,  
> > and/or do a xpath search to retrieve images. Check hpricot .
> 
> Yeah, it is quite easy with Hpricot:
> 
> require ‘open-uri’  
> require ‘hpricot’
> 
> site =  
> Hpricot(open(“[http://code.google.com/edu/submissions/SedgewickWayne/index.html](http://code.google.com/edu/submissions/SedgewickWayne/index.html)”))  
> site.search(“//img”) #=\> returns an array of all images

yes i used as this  
doc = Hpricot.parse(item.description)  
imgs = doc.search(“//img”)  
@src\_array = imgs.collect{|img|img.attributes[“src”]}

but it gives only the Image Url’s but I need to Get  
 ![]() tag Fully …  
Any Helps

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/regexp-help/146465/5 "2008-08-21T13:45:45Z")

</div>

Newb N. schrieb:

> > require ‘open-uri’  
> > doc = Hpricot.parse(item.description)  
> > imgs = doc.search("//img")  
> > @src\_array = imgs.collect{|img|img.attributes[“src”]}
> 
> but it gives only the Image Url’s but I need to Get  
> ![]() tag Fully …  
> Any Helps

Then do

@src\_array = imgs.collect{|img| “\<img src =”#{img.attributes[“src”]  
}"\>" }

?

–  
Otto Software Partner GmbH

Jan P. (e-mail: removed\_email\_address@domain.invalid)

Tel. 0351/49723202, Fax: 0351/49723119  
01067 Dresden, Freiberger StraÃŸe 35 - AG Dresden, HRB 2475  
GeschÃ¤ftsfÃ¼hrer: Burkhard Arrenberg, Heinz A. Bade, Jens Gruhl

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 21, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/regexp-help/146465/6 "2008-08-21T14:04:05Z")

</div>

i’m not really sure about hpricot , but with html/tree parser , when you  
call a node’s to\_s method , you got it’s full html . So , you should try  
to call .to\_s on the array’s elements , and see if it’s what you need.

---

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [August 22, 2008, 6:12am UTC](https://www.ruby-forum.com/t/regexp-help/146465/7 "2008-08-22T06:12:17Z")

</div>

Jan P. wrote:

> Newb N. schrieb:
> 
> > > require ‘open-uri’  
> > > doc = Hpricot.parse(item.description)  
> > > imgs = doc.search("//img")  
> > > @src\_array = imgs.collect{|img|img.attributes[“src”]}
> > 
> > but it gives only the Image Url’s but I need to Get  
> > ![]() tag Fully …  
> > Any Helps
> 
> Then do
> 
> @src\_array = imgs.collect{|img| “\<img src =”#{img.attributes[“src”]  
> }"\>" }
> 
> ?

yes It works…  
Is It Possible to Use @src\_array into String.sub!(pattern,replacement)  
That is

@src\_array.sub(/[@src\_array]/," ")

@src\_array contains all the img tags.i need to replace it empty…  
for that will tat above code work?  
can u get me there?

> –  
> Otto Software Partner GmbH
> 
> Jan P. (e-mail: removed\_email\_address@domain.invalid)
> 
> Tel. 0351/49723202, Fax: 0351/49723119  
> 01067 Dresden, Freiberger StraÃŸe 35 - AG Dresden, HRB 2475  
> GeschÃ¤ftsfÃ¼hrer: Burkhard Arrenberg, Heinz A. Bade, Jens Gruhl
