Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regular Expressions

Reply
Thread Tools

Regular Expressions

 
 
Newb Newb
Guest
Posts: n/a
 
      08-25-2008
I have a image url like this <img src
="http://www.ingolfwetrust.com/golf-central/content/binary/Davis-Love-III.jpg">

From this url i need to extract only the picture name using regular
Expressions.
That is I Want to Extract like this => Davis-Love-III.jpg.
Any Advices
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
James Coglan
Guest
Posts: n/a
 
      08-25-2008
[Note: parts of this message were removed to make it a legal post.]

2008/8/25 Newb Newb <(E-Mail Removed)>

> I have a image url like this <img src
> ="
> http://www.ingolfwetrust.com/golf-ce...s-Love-III.jpg
> ">
>
> From this url i need to extract only the picture name using regular
> Expressions.
> That is I Want to Extract like this => Davis-Love-III.jpg.
> Any Advices




# url = "
http://www.ingolfwetrust.com/golf-central/content/binary/Davis-Love-III.jpg"
filename = url.scan(/[^\/]+/).last

This looks for all sections of the string that do not contain a slash and
picks the last one.

 
Reply With Quote
 
 
 
 
Newb Newb
Guest
Posts: n/a
 
      08-25-2008
>
> # url = "
> http://www.ingolfwetrust.com/golf-central/content/binary/Davis-Love-III.jpg"
> filename = url.scan(/[^\/]+/).last
>
> This looks for all sections of the string that do not contain a slash
> and
> picks the last one.



Thanks for the reply..it works but my url is like this

<img src
="http://www.ingolfwetrust.com/golf-central/content/binary/Davis-Love-III.jpg">
- <img src
="http://www.ingolfwetrust.com/golf-central/aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf">

so it gives me only this

aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf">


Pls Help me on this


--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Lex Williams
Guest
Posts: n/a
 
      08-25-2008
> Thanks for the reply..it works but my url is like this
>
> <img src
> ="http://www.ingolfwetrust.com/golf-central/content/binary/Davis-Love-III.jpg">
> - <img src
> ="http://www.ingolfwetrust.com/golf-central/aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf">
>
> so it gives me only this
>
> aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf">
>
>
> Pls Help me on this


What do you want to do with
aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf ? Are you using it ?
If not , for the rest of the pictures , you could be calling
File.basename on them and get the last part only . For example , on
http://www.ingolfwetrust.com/golf-ce...s-Love-III.jpg
you will receive Davis-Love-III.jpg .
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Newb Newb
Guest
Posts: n/a
 
      08-25-2008
> What do you want to do with
> aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf ? Are you using it ?
> If not , for the rest of the pictures , you could be calling
> File.basename on them and get the last part only . For example , on
> http://www.ingolfwetrust.com/golf-ce...s-Love-III.jpg
> you will receive Davis-Love-III.jpg .



sorry yar ..Nothing Work On That.I get nil value

Any ways i used this expression to extract the imgage url =>
(/<img.*?>/)
It has given me image url.
But I want to get all the url's based on file extensions like jpg and
png..
That Is... if it contains image file extension,Only those image urls
has to fetched


Help Me
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Lee Jarvis
Guest
Posts: n/a
 
      08-25-2008
#!/usr/bin/ruby

require 'rubygems'
require 'hpricot'

ext = %w( .jpg .png .gif ... )
Hpricot(DATA.read).search("img") do |img|
name = File.basename(img.attributes['src'])
puts name if ext.include?(File.extname(name))
end

__END__
[~]$ blah
<img src="http://host.com/foo.jpg" />
<img src="http://ruby-lang.org/something/path/to/image.jpg" />
<img src="http://google.com/PNgdvsbajkDbnm976" />


#=>
foo.jpg
image.jpg
 
Reply With Quote
 
Lee Jarvis
Guest
Posts: n/a
 
      08-25-2008
#!/usr/bin/ruby

require 'rubygems'
require 'hpricot'

ext = %w( .jpg .png .gif ... )
Hpricot(DATA.read).search("img") do |img|
name = File.basename(img.attributes['src'])
puts name if ext.include?(File.extname(name))
end

__END__
[~]$ blah
<img src="http://host.com/foo.jpg" />
<img src="http://ruby-lang.org/something/path/to/image.jpg" />
<img src="http://google.com/PNgdvsbajkDbnm976" />
 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      08-25-2008
On Mon, Aug 25, 2008 at 2:13 PM, Newb Newb <(E-Mail Removed)> wrote:
>> What do you want to do with
>> aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf ? Are you using it ?
>> If not , for the rest of the pictures , you could be calling
>> File.basename on them and get the last part only . For example , on
>> http://www.ingolfwetrust.com/golf-ce...s-Love-III.jpg
>> you will receive Davis-Love-III.jpg .

>
>
> sorry yar ..Nothing Work On That.I get nil value
>
> Any ways i used this expression to extract the imgage url =>
> (/<img.*?>/)
> It has given me image url.
> But I want to get all the url's based on file extensions like jpg and
> png..
> That Is... if it contains image file extension,Only those image urls
> has to fetched


Another way to extract the URL and get only the path:

require 'uri'

tag = '<img src="http://www.ingolfwetrust.com/golf-central/aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf">'
File.basename(URI.split(URI.extract(b)[0])[5])
=> "aggbug.ashx"

Then you can check with File.extname to check for the extensions you
want, as others have shown.

Hope this gives you other ideas,

Jesus.

 
Reply With Quote
 
Newb Newb
Guest
Posts: n/a
 
      08-27-2008
Hi all ..
I redefine my Question now..i want to get all the image url which has
jpg .png file extensions using regular expressions ..
Any Advices...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Damjan Rems
Guest
Posts: n/a
 
      08-27-2008
Newb Newb wrote:
>>
>> # url = "
>> http://www.ingolfwetrust.com/golf-central/content/binary/Davis-Love-III.jpg"
>> filename = url.scan(/[^\/]+/).last
>>
>> This looks for all sections of the string that do not contain a slash
>> and
>> picks the last one.

>
>
> Thanks for the reply..it works but my url is like this
>
> <img src
> ="http://www.ingolfwetrust.com/golf-central/content/binary/Davis-Love-III.jpg">
> - <img src
> ="http://www.ingolfwetrust.com/golf-central/aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf">
>
> so it gives me only this
>
> aggbug.ashx?id=56f9e33a-6333-4daf-b4e1-137ddc051adf">
>
>
> Pls Help me on this


Althow it is not the most genius one

url.split("/").last.gsub(/"|>/,'')


by
TheR
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Regular Expressions in ASP.net Jay Douglas ASP .Net 3 11-03-2003 08:09 PM
Regular expressions mark Perl 4 10-28-2003 12:37 PM
perl regular expressions return last matched occurence? Dustin D. Perl 1 08-28-2003 01:51 AM
matching curly braces and regular expressions Dustin D. Perl 0 08-26-2003 11:18 PM
Add custom regular expressions to the validation list of available expressions Jay Douglas ASP .Net 0 08-15-2003 10:19 PM



Advertisments