How to grep the shortly matching in a string

How to grep the shortly matching in a string

Post by Arowana Li » Mon, 19 Dec 2005 19:37:48


I used regular expression to grep content from a web page,but it seems
ruby always match the longest string,but I need to fetch the shortly
matching. How can I do ?
Thanks for help!

--
Posted via http://www.yqcomputer.com/
 
 
 

How to grep the shortly matching in a string

Post by Arowana Li » Mon, 19 Dec 2005 20:26:12


Thanks Robert!
here is the example.
<tr><td>Format</td></tr><tr><td>Format2</td></tr><tr><td>Format3</td></tr>
I use the code below to fetch "Format" and "Format2" in the table
feature=content.scan(/[<tr><td>([\w\s])*<\/td><\/tr>/)
I want to get each row into array feature,like
feature[0]=Format;feature[1]=Format2...
but it match all the row into
feature[0]=Format</td></tr><tr><td>Format2</td></tr><tr><td>Format3


--
Posted via http://www.yqcomputer.com/

 
 
 

How to grep the shortly matching in a string

Post by Ross Bamfo » Mon, 19 Dec 2005 22:23:48

On Sun, 18 Dec 2005 11:26:12 -0000, Arowana Lin < XXXX@XXXXX.COM >




Non greedy quantifiers could probably be used to do this, but given that
your data is quite nicely delimited you may as well just scan ;)

s =
"<tr><td>Format</td></tr><tr><td>Format2</td></tr><tr><td>Format3</td></tr>"
s.scan(/<td>([^<]*)<\/td>/) { |it| puts it }

outputs:

Format
Format2
Format3
=>
"<tr><td>Format</td></tr><tr><td>Format2</td></tr><tr><td>Format3</td></tr>"

Obviously it doesn't do quite what you want (you need an array) but that
part should be easy to add...

--
Ross Bamford - XXXX@XXXXX.COM
 
 
 

How to grep the shortly matching in a string

Post by Eero Sayna » Mon, 19 Dec 2005 22:41:09


Yes, you want the non-greedy version .*? instead of .*
there. You can use ? with the *, + and {,} specifiers.


E

--
Posted via http://www.yqcomputer.com/
 
 
 

How to grep the shortly matching in a string

Post by dblac » Tue, 20 Dec 2005 01:45:49

Hi --




array.each {|it| puts it } == puts array :-)


scan returns an array, so just grab it:

results = s.scan(/.../).flatten # flatten because of the ()'s

[And yes, everyone who's about to say it, we all know that you cannot
parse arbitrary HTML with a single regular expression.]


David

--
David A. Black
XXXX@XXXXX.COM

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.yqcomputer.com/
 
 
 

How to grep the shortly matching in a string

Post by Ross Bamfo » Tue, 20 Dec 2005 02:22:44


I _knew_ I was missing something.

In mitigation, I'd like to say that it was my first mail-check of the day.
Unfortunately, it makes no difference because I'd completely forgotten
about scan (etc) with no block anyway, so I had it returning the string :P

Thanks, David :)

--
Ross Bamford - XXXX@XXXXX.COM
 
 
 

How to grep the shortly matching in a string

Post by Devin Mull » Tue, 20 Dec 2005 02:29:47


Maybe in Perl 6! http://www.yqcomputer.com/

Devin
And a shoutout to Rubyful Soup is in order, I think...