# Still Query Continues

**URL:** <https://www.ruby-forum.com/t/still-query-continues/146845>\
**Category:** Ruby\
**Created:** [August 26, 2008, 6:52am UTC](https://www.ruby-forum.com/t/still-query-continues/146845 "2008-08-26T06:52:48Z")\
**Posts on this page:** 4\
**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 26, 2008, 6:52am UTC](https://www.ruby-forum.com/t/still-query-continues/146845/1 "2008-08-26T06:52:48Z")

</div>

Thanks for taking time for Reply.  
My Problem Has not Sorted Out.Actually i m Extracting These images  
from other Webpages.  
Using Hpricot i Got all the Image urls.I did code like below

doc = Hpricot.parse(item.description)  
imgs = doc.search("//img")  
@src\_array = imgs.collect{|img|"\<img src  
="#{img.attributes[“src”]}"\>" }

It has given all the image url.But for example i have image urls even  
like below

![](http://www.ingolfwetrust.com/golf-central/content/binary/Craig-Stadler-Davis-Love-II.jpg)

- ![](http://www.ingolfwetrust.com/golf-central/aggbug.ashx?id=0ab690de-a81c-4470-8539-7f9ce0f75ee3)

---

<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 26, 2008, 11:21am UTC](https://www.ruby-forum.com/t/still-query-continues/146845/2 "2008-08-26T11:21:10Z")

</div>

On 26.08.2008, at 06:48, Newb N. wrote:

> In this case the first one is valid image url because it has jpg  
> file  
> extensions.so i need to display the image urls which has the  
> .jpg,.png,gif extensions only…
> 
> Is this possible using Regular Expressions?

yes.

irb\> a=%w{abc.jpg def ghi.png jkl.pngjpg mnp.bpng}  
=\> [“abc.jpg”, “def”, “ghi.png”, “jkl.pngjpg”, “mnp.bpng”]  
irb\> a.select {|w| w.match(/.(png|jpg)?$/)}  
=\> [“abc.jpg”, “ghi.png”]

> Pls Help me out To Understand

If you want to understand more then you should read more  
documentation and  
wikipedia on your topic. Also test carefully your expressions by an irb  
session. Often it helps if you write a simple test to understand your  
problem.

In my opinion a great ressource for regular expressions is

> **[Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp...](https://www.regular-expressions.info)**
>
> At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions.

Maybe the wikibook with the topic ruby will help you in the future:  
[http://en.wikibooks.org/wiki/Ruby\_Programming](http://en.wikibooks.org/wiki/Ruby_Programming)

Please read the fine manuals in the web before writing forum entries.

regards, Sandor  
Szücs

---

<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 27, 2008, 8:09am UTC](https://www.ruby-forum.com/t/still-query-continues/146845/3 "2008-08-27T08:09:52Z")

</div>

Again I have to start from the scratch…From The beginning itself i got  
into problem.Actually my problem is I want to extract the image tag  
which contains image file extensions like .jpg .png.But currently i m  
using this RegEx (/\<img.\*?\>/).But it gives me img tags without .jpg or  
.png file extensions.  
So pls Kindly Help Me All…I m really struggling.  
You People Favour me and thanks much.

---

<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 28, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/still-query-continues/146845/4 "2008-08-28T14:06:54Z")

</div>

On 27.08.2008, at 08:05, Newb N. wrote:

> Again I have to start from the scratch…From The beginning itself i  
> got  
> into problem.Actually my problem is I want to extract the image tag  
> which contains image file extensions like .jpg .png.

You define the last part of your target: ends with .jpg or .png

> But currently i m  
> using this RegEx (/\<img.\*?\>/).But it gives me img tags without .jpg or  
> .png file extensions.

That regular expression doesn’t match the end: .jpg or .png

Think about what your regex will match. Try it yourself with irb.  
You want to match an url string with a specific ending.  
Try to match the start, the end and all characters between, without  
matching  
characters pre and post of your target.

Your string looks like that " ![test](http://host.domain.tld/path/to/%0Apic.png)  
What you want to match is just [http://host.domain.tld/path/to/pic.png](http://host.domain.tld/path/to/pic.png)

What characters are allowed in your target?  
What substrings should be a part of the string you want to match?  
What characters are the bounces that you don’t want to match?

Please try to solve the problem yourself. You should learn to think  
about the  
problem you want to solve, but I have included one solution.

Don’t just copy 'n paste a solution. 😉

regards, Sandor  
Szücs–

P.S.  
What characters are allowed in your target?  
a-zA-Z0-9:\_%-./  
What substrings should be a part of the string you want to match?  
the last part have to be: .png or .jpg or .gif  
What characters are the bounces that you don’t want to match?  
In my example above it is ’ . Is this character a part of our target?  
no!

here an example:  
I downloaded the html file of [http://rubyforge.org/](http://rubyforge.org/)

irb\> s=File.read(“RubyForge\_ Welcome.html”)  
irb(main):014:0\> s.split(’ ').select do |w|  
irb(main):015:1\* t= w.match(/[a-zA-Z0-9\_:%-/.]\*.(png|jpg|gif)/)  
irb(main):016:1\> puts t if t  
irb(main):017:1\> end  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
header-bg.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
controls-bg.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
tabs-bg.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
inner-tabs-bg.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
active-tab-bg.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
active-inner-tab-bg.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
bottom-fade-bg.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
controls-left.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
header.png  
/images/lsrc\_2008\_logo.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
clear.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
clear.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
clear.png  
[http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/](http://static.rubyforge.vm.bytemark.co.uk/themes/rubyforge/images/)  
clear.png
