Hpricot and Regular expression

  • Thread starter Dhanasekaran Vivekanandhan
  • Start date
D

Dhanasekaran Vivekanandhan

Hi All,
I have a html fragment like the following

<a href="forumdisplay.php?f=131"><strong>Toon Zone News1</strong></a>
<a href="forumdisplay.php?f=132"><strong>Toon Zone News2</strong></a>
<a href="forumdisplay.php?f=133"><strong>Toon Zone News3</strong></a>
<a href="forumdisplay.php?f=abcd"><strong>Toon Zone News3</strong></a>

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"))
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
 
P

Peter Szinek

Dhanasekaran said:
Hi All,
I have a html fragment like the following

<a href="forumdisplay.php?f=131"><strong>Toon Zone News1</strong></a>
<a href="forumdisplay.php?f=132"><strong>Toon Zone News2</strong></a>
<a href="forumdisplay.php?f=133"><strong>Toon Zone News3</strong></a>
<a href="forumdisplay.php?f=abcd"><strong>Toon Zone News3</strong></a>

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.

Try

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

HTH,
Peter
__
http://www.rubyrailways.com
 

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

Forum statistics

Threads
474,221
Messages
2,571,133
Members
47,747
Latest member
swapote

Latest Threads

Top