Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > not greedy enough

Reply
Thread Tools

not greedy enough

 
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      09-22-2004
Honestly, I really dislike regular expressions. Some people love them for
their terseness. I, on the other hand, being a pooh of very little brain,
spend far too many hair pulling hours just trying to get one of the dang
things right. Ugh.

Right now I'm working on this:

/^[ ]*[-](.*\n)+.*\n/

Applied to:

%Q{
- xyz
- abc

- not this
}

I want it to match just the first section, essentially

" - xyz\n - abc\n\n".

But it's matching the whole string. Can any of you Regexp experts fill me in?

Thanks,
T.


 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      09-22-2004
Hi --

On Thu, 23 Sep 2004, trans. (T. Onoma) wrote:

> Right now I'm working on this:
>
> /^[ ]*[-](.*\n)+.*\n/
>
> Applied to:
>
> %Q{
> - xyz
> - abc
>
> - not this
> }
>
> I want it to match just the first section, essentially
>
> " - xyz\n - abc\n\n".
>
> But it's matching the whole string. Can any of you Regexp experts fill me in?


I think what you want is: match one or more consecutive occurrences
of <space>-<stuff>\n, stopping when one of them has an extra \n at the
end.

Try this and see if it's close to what you need:

/(^\ *-.*\n)+\n/


David

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
 
Hal Fulton
Guest
Posts: n/a
 
      09-22-2004
David A. Black wrote:
> I think what you want is: match one or more consecutive occurrences
> of <space>-<stuff>\n, stopping when one of them has an extra \n at the
> end.
>
> Try this and see if it's close to what you need:
>
> /(^\ *-.*\n)+\n/


Umm... in the regexp as in the other, isn't an m needed at the end?


Hal



 
Reply With Quote
 
Hal Fulton
Guest
Posts: n/a
 
      09-22-2004
Hal Fulton wrote:
> David A. Black wrote:
>
>> I think what you want is: match one or more consecutive occurrences
>> of <space>-<stuff>\n, stopping when one of them has an extra \n at the
>> end.
>>
>> Try this and see if it's close to what you need:
>>
>> /(^\ *-.*\n)+\n/

>
>
> Umm... in the regexp as in the other, isn't an m needed at the end?


No, sorry, ignore me. I was quite wrong.

Hal




 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      09-22-2004
Hi --

On Thu, 23 Sep 2004, Hal Fulton wrote:

> David A. Black wrote:
> > I think what you want is: match one or more consecutive occurrences
> > of <space>-<stuff>\n, stopping when one of them has an extra \n at the
> > end.
> >
> > Try this and see if it's close to what you need:
> >
> > /(^\ *-.*\n)+\n/

>
> Umm... in the regexp as in the other, isn't an m needed at the end?


No, because if you put an /m, then the dot will match \n, and .* will
trample over the end of the lines. I'm using \n as a flag for
starting and stopping.


David

--
David A. Black
(E-Mail Removed)



 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      09-22-2004
On Wednesday 22 September 2004 03:36 pm, David A. Black wrote:
> I think what you want is: match one or more consecutive occurrences
> of <space>-<stuff>\n, stopping when one of them has an extra \n at the
> end.
>
> Try this and see if it's close to what you need:
>
> /(^\ *-.*\n)+\n/


/(^\ *-.*\n)+\ *\n/

Perfect. A "many hairs" thanks!

T.



 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      09-22-2004
trans. (T. Onoma) schrieb:
> Right now I'm working on this:
>
> /^[ ]*[-](.*\n)+.*\n/
>
> Applied to:
>
> %Q{
> - xyz
> - abc
>
> - not this
> }
>
> I want it to match just the first section, essentially
>
> " - xyz\n - abc\n\n".
>
> But it's matching the whole string. Can any of you Regexp experts fill me in?


The repeating group in your regexp is (.*\n)+ and this matches every line. You
have to enlarge this group as in /^( *-.*\n)+.*\n/

HTH

Pit


 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      09-23-2004
On Wednesday 22 September 2004 03:55 pm, Pit Capitain wrote:
> The repeating group in your regexp is (.*\n)+ and this matches every line.
> You have to enlarge this group as in /^( *-.*\n)+.*\n/


That makes sense. Thanks all!

T.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Greedy and non greedy quantifiers Dan Kelly Ruby 4 01-19-2008 08:36 PM
Enough is enough... Imhotep Computer Security 16 09-28-2005 03:36 PM
greedy v. non-greedy matching Matt Garrish Perl Misc 4 02-16-2004 03:25 PM
Enough is enough.... ajacobs2 Digital Photography 33 10-05-2003 12:14 PM
Resolution - when is Enough ENOUGH? (a personal view) VT Digital Photography 43 09-12-2003 11:15 AM



Advertisments