From: dblack@...
Date: 2005-12-19T01:45:49+09:00
Subject: Re: How to grep the shortly matching in a string
Hi --
On Sun, 18 Dec 2005, Ross Bamford wrote:
> 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 =
> "
| Format |
| Format2 |
| Format3 |
"
> s.scan(/([^<]*)<\/td>/) { |it| puts it }
array.each {|it| puts it } == puts array :-)
> outputs:
>
> Format
> Format2
> Format3
> =>
> " | | Format |
| Format2 |
| Format3 |
"
>
> Obviously it doesn't do quite what you want (you need an array) but that part
> should be easy to add...
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
dblack@wobblini.net
"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black