Regular Expressions

Discussion in 'Ruby' started by Newb Newb, Aug 25, 2008.

  1. Newb Newb

    Newb Newb Guest

    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/.
     
    Newb Newb, Aug 25, 2008
    #1
    1. Advertising

  2. Newb Newb

    James Coglan Guest

    [Note: parts of this message were removed to make it a legal post.]

    2008/8/25 Newb Newb <>

    > 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




    # 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.
     
    James Coglan, Aug 25, 2008
    #2
    1. Advertising

  3. Newb Newb

    Newb Newb Guest

    >
    > # 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/.
     
    Newb Newb, Aug 25, 2008
    #3
  4. Newb Newb

    Lex Williams Guest

    > 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-central/content/binary/Davis-Love-III.jpg
    you will receive Davis-Love-III.jpg .
    --
    Posted via http://www.ruby-forum.com/.
     
    Lex Williams, Aug 25, 2008
    #4
  5. Newb Newb

    Newb Newb Guest

    > 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-central/content/binary/Davis-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/.
     
    Newb Newb, Aug 25, 2008
    #5
  6. Newb Newb

    Lee Jarvis Guest

    #!/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
     
    Lee Jarvis, Aug 25, 2008
    #6
  7. Newb Newb

    Lee Jarvis Guest

    #!/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" />
     
    Lee Jarvis, Aug 25, 2008
    #7
  8. On Mon, Aug 25, 2008 at 2:13 PM, Newb Newb <> 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-central/content/binary/Davis-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.
     
    Jesús Gabriel y Galán, Aug 25, 2008
    #8
  9. Newb Newb

    Newb Newb Guest

    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/.
     
    Newb Newb, Aug 27, 2008
    #9
  10. Newb Newb

    Damjan Rems Guest

    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/.
     
    Damjan Rems, Aug 27, 2008
    #10
  11. Newb Newb

    Newb Newb Guest

    my Question is...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/.
     
    Newb Newb, Aug 28, 2008
    #11
  12. Newb Newb

    Phlip Guest

    Newb Newb wrote:
    > my Question is...i want to get all the image url which has .jpg .png
    > file extensions using regular expressions ..


    How did you do with the pattern we already suggested, with ".*?.jpg" in it?

    Can you run another pass with ".*?.png" too?
     
    Phlip, Aug 28, 2008
    #12
  13. Newb Newb

    Newb Newb Guest

    Phlip wrote:
    > Newb Newb wrote:
    >> my Question is...i want to get all the image url which has .jpg .png
    >> file extensions using regular expressions ..

    >
    > How did you do with the pattern we already suggested, with ".*?.jpg" in
    > it?
    >
    > Can you run another pass with ".*?.png" too?


    could you pls give me the full regular expressions to do that?
    --
    Posted via http://www.ruby-forum.com/.
     
    Newb Newb, Aug 28, 2008
    #13
  14. Newb Newb

    Phlip Guest

    >> Can you run another pass with ".*?.png" too?
    >
    > could you pls give me the full regular expressions to do that?


    What happened to the ones people here posted?
     
    Phlip, Aug 28, 2008
    #14
  15. Newb Newb

    Newb Newb Guest

    Phlip wrote:
    >>> Can you run another pass with ".*?.png" too?

    >>
    >> could you pls give me the full regular expressions to do that?

    >
    > What happened to the ones people here posted?


    hi..
    I Got it work.Thank u for taking time to reply
    --
    Posted via http://www.ruby-forum.com/.
     
    Newb Newb, Aug 28, 2008
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Douglas

    Custom Regular Expressions in ASP.net

    Jay Douglas, Nov 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    619
    mikeb
    Nov 3, 2003
  2. mark

    Regular expressions

    mark, Jun 30, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,737
  3. Dustin D.
    Replies:
    1
    Views:
    11,384
  4. Jay Douglas
    Replies:
    0
    Views:
    644
    Jay Douglas
    Aug 15, 2003
  5. Noman Shapiro
    Replies:
    0
    Views:
    262
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page