# Hpricot and Regular expression

**URL:** <https://www.ruby-forum.com/t/hpricot-and-regular-expression/79806>\
**Category:** Ruby\
**Created:** [December 19, 2006, 8:18am UTC](https://www.ruby-forum.com/t/hpricot-and-regular-expression/79806 "2006-12-19T08:18:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhanasekara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@dhanasekara](https://www.ruby-forum.com/u/dhanasekara)\
**Post date:** [December 19, 2006, 8:18am UTC](https://www.ruby-forum.com/t/hpricot-and-regular-expression/79806/1 "2006-12-19T08:18:54Z")

</div>

Hi All,  
I have a html fragment like the following

**Toon Zone News1**  
**Toon Zone News2**  
**Toon Zone News3**  
**Toon Zone News3**

I want to match only the first three anchor tags, I dont want to get the  
last one since the href f parameter is abcd.it is not an integer. I want  
to get only if the request parameter is integer.i.e. the first three  
anchor tags.

I have following code

doc = Hpricot(open(“[http://0.0.0.0:3000/dh/list](http://0.0.0.0:3000/dh/list)”))  
fun =  
doc.search(“//a[@href=‘forumdisplay.php?f=131’]//strong”).inner\_html  
puts fun

but it will fetch the first anchor tag content only.so I think I need to  
use some regular expression to match 131, 132, 133 (f parameter) values.  
I dont know how to do.  
any help would be appreciated.  
thanks,  
dhanasekaran

---

<div class="post-metadata">

**Author:** ![dhanasekara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@dhanasekara](https://www.ruby-forum.com/u/dhanasekara)\
**Post date:** [December 20, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/hpricot-and-regular-expression/79806/2 "2006-12-20T18:35:49Z")

</div>

–  
Rien ne peut jamais marcher si l’on songe à tout ce qu’il faut pour que  
çamarche.  
– Daniel Pennac

here is a way (I uess not optimal, I’m really new here)

#!/usr/bin/ruby  
require ‘hpricot’

html = \<\<EOS

**Toon Zone News1**** Toon Zone News2 ****Toon Zone News3**** Toon Zone News3** EOS

doc = Hpricot(html)  
result=[]  
#get each node  
doc.search(“//a”).each do |chaque|  
 #keep only those who have the correct value for attribute as set by the  
regexp  
result \<\< chaque.at(“//strong”).inner\_html if  
chaque.get\_attribute(“href”) =~  
/f=13?/  
end

 

Hope this helps  
Sylvain

 

Selon Dhanasekaran V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![dhanasekara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@dhanasekara](https://www.ruby-forum.com/u/dhanasekara)\
**Post date:** [December 20, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/hpricot-and-regular-expression/79806/3 "2006-12-20T19:18:39Z")

</div>

Dhanasekaran V. wrote:

> to get only if the request parameter is integer.i.e. the first three  
> anchor tags.

Try

result = (Hpricot(html)/“a[@href]”).map.reject { |elem|  
elem.attributes[‘href’] !~ /=\d+$/ }

HTH,  
Peter  
\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![dhanasekara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@dhanasekara](https://www.ruby-forum.com/u/dhanasekara)\
**Post date:** [December 20, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/hpricot-and-regular-expression/79806/4 "2006-12-20T19:22:49Z")

</div>

On 12/19/06, Sylvain T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[snip]

> #keep only those who have the correct value for attribute as set by the regexp  
> result \<\< chaque.at(“//strong”).inner\_html if chaque.get\_attribute(“href”) =~  
> /f=13?/  
> end  
> [snip]

for matching f=integer… then use this regexp

/\bf=\d+\b/
