Regex question

So I am scanning through chat logs looking for messages where I
mentioned "points". My current regex is:
text.scan /^(.*)([-+]?[0-9]+\s+points?)(.*)$/i
So I'd like each match in 3 groups:
1. The message up until the part containing the points
2. The points part. This could have a + or - prefix, 1 or more digits,
and an optional s after point
3. The rest of the message line.

My problem is, the (.*) is eating up parts that I would like to appear
in group 2. I just bought a book on regex but I'm not very far yet :frowning:
Any help would be appreciated!

···

--
Posted via http://www.ruby-forum.com/.

You could try matching anything that's not a -, + or number in your first
group, e.g.

([^-+0-9]*)

Note that I'm assuming Ruby uses Perl Compatible Regular Expressions

Cheers,

Paul

···

On Wed, Feb 20, 2008 at 8:15 PM, J. Cooper <nefigah@gmail.com> wrote:

So I am scanning through chat logs looking for messages where I
mentioned "points". My current regex is:
text.scan /^(.*)([-+]?[0-9]+\s+points?)(.*)$/i
So I'd like each match in 3 groups:
1. The message up until the part containing the points
2. The points part. This could have a + or - prefix, 1 or more digits,
and an optional s after point
3. The rest of the message line.

My problem is, the (.*) is eating up parts that I would like to appear
in group 2. I just bought a book on regex but I'm not very far yet :frowning:
Any help would be appreciated!
--
Posted via http://www.ruby-forum.com/\.

J. Cooper wrote:

My current regex is:
text.scan /^(.*)([-+]?[0-9]+\s+points?)(.*)$/i
[...]
My problem is, the (.*) is eating up parts that I would like to appear
in group 2.

Make it non-greedy: (.*?)

HTH,
Sebastian

···

--
NP: Anathema - Cries On The Wind
Jabber: sepp2k@jabber.org
ICQ: 205544826

Paul wrote:

You could try matching anything that's not a -, + or number in your
first
group, e.g.

([^-+0-9]*)

Note that I'm assuming Ruby uses Perl Compatible Regular Expressions

Cheers,

Paul

But then a message like:
"I'm 3 years old and have 80 points"
wouldn't come out right, correct?

···

--
Posted via http://www.ruby-forum.com/\.

Make it non-greedy: (.*?)

HTH,
Sebastian

Life saver. Thanks!

···

--
Posted via http://www.ruby-forum.com/\.