[Ruby] regex question

Aaron Patterson aaron_patterson at speakeasy.net
Mon Feb 12 17:38:06 PST 2007


On Mon, Feb 12, 2007 at 05:30:50PM -0800, shaners becker wrote:
> closer.
> 
> that found a result. but only one result. it found from the first  
> <recommendation> all the way through the last </recommendation>  
> ignoring all the rest along the way
> 
> the document is structured like so:
> 
> book --1
> 	recommendation  --1
> 		book  --1a
> 		book  --1b
> 		book  --1c
> 	/recommendation  --1
> /book --1
> book --2
> 	recommendation  --2
> 		book  --2a
> 		book  --2b
> 		book  --2c
> 	/recommendation  --2
> /book --2
> 
> and so on.
> 
> > <recommendations>(?m).*</recommendations>
> 
> finds from recommendation  --1 all the way to /recommendation --2  
> leaving only one book with no recommendations. instead of two books  
> with no recommendations.

Thats because the match was greedy.  Try this:

  <recommendations>(?m).*?</recommendations>

Or this:

  <recommendations>(?m)[^>]*</recommendations>

-- 
Aaron Patterson
http://tenderlovemaking.com/


More information about the Ruby mailing list