REGEXP HELP

N

Newb Newb

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?
 
L

Lex Williams

Newb said:
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 .
 
N

Newb Newb

Thomas said:
Yeah, it is quite easy with Hpricot:

require 'open-uri'
require 'hpricot'

site =
Hpricot(open("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
<img src =" "> tag Fully ...
Any Helps
 
J

Jan Pilz

Newb said:
Thomas said:
Yeah, it is quite easy with Hpricot:

require 'open-uri'
require 'hpricot'

site =
Hpricot(open("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
<img src =" "> tag Fully ...
Any Helps
Then do

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

?


--
Otto Software Partner GmbH

Jan Pilz (e-mail: (e-mail address removed))

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
 
L

Lex Williams

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.
 
N

Newb Newb

Jan said:
Newb said:
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
<img src =" "> 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top