From: luke (dot) Date: 2005-09-05T13:11:28+09:00 Subject: Re: Making my Regex less greedy? Thanks very much, that works a treat. Always nice to have something demonstrated in Lewis Carroll. So, {100,#{m.length}}? effectively is now finding the first match, if any .... Out of curiosity, is it easy to express "find the 3rd match"?. (Rather than saying "find 3 matches"). Luke "Gavin Kistner" wrote in message news:3844E43F-DA57-489D-9DA1-4E5FF239A296@refinery.com... > On Sep 4, 2005, at 5:51 PM, luke wrote: > > The logic I'd like the above regex to operate with is: "Starting 100 > > characters into the message, chop the message at the next paragraph > > break". > > The question mark makes quantifiers non-greedy. > + versus .+? > * versus .*? > {a,b} versus .{a,b}? > > For example: > txt = < Twas brillig > and the slithy toves > did gyre and gimble > in the wabe > END > > def truncate_after( str, length ) > str =~ /\A(.{#{length},}?)\n(.+)/m > return [ $1, $2 ] > end > > p truncate_after( txt, 0 ) > #=>["Twas brillig", "and the slithy toves\ndid gyre and gimble\nin > the wabe\n"] > > p truncate_after( txt, 20 ) > #=>["Twas brillig\nand the slithy toves", "did gyre and gimble\nin > the wabe\n"] > > p truncate_after( txt, 40 ) > #=>["Twas brillig\nand the slithy toves\ndid gyre and gimble", "in > the wabe\n"] > > >