not greedy enough

T

trans. (T. Onoma)

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.
 
D

David A. Black

Hi --

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
 
H

Hal Fulton

David said:
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
 
D

David A. Black

Hi --

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
 
T

trans. (T. Onoma)

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.
 
P

Pit Capitain

trans. (T. Onoma) said:
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
 
T

trans. (T. Onoma)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,089
Members
48,773
Latest member
Kaybee

Latest Threads

Top