From: Ross Bamford Date: 2005-12-18T22:32:46+09:00 Subject: Re: How to grep the shortly matching in a string On Sun, 18 Dec 2005 11:26:12 -0000, Arowana Lin wrote: > Robert Retzbach wrote: >> Arowana Lin schrieb: >> >>> 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! >>> >>> >>> >> Would be nice to see the regexp. Maybe using non-greedy multipliers will >> do. >> You could also use character classes like [^<>] to match chars between < >> and >. >> But I don't know what you want to match, so please show me :> > > Thanks Robert! > here is the example. > FormatFormat2Format3 > I use the code below to fetch "Format" and "Format2" in the table > feature=content.scan(/[([\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]=FormatFormat2Format3 > > 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 = "FormatFormat2Format3" s.scan(/([^<]*)<\/td>/) { |it| puts it } outputs: Format Format2 Format3 => "FormatFormat2Format3" Obviously it doesn't do quite what you want (you need an array) but that part should be easy to add... -- Ross Bamford - rosco@roscopeco.remove.co.uk