regex question

J

J. Cooper

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 :(
Any help would be appreciated!
 
P

Paul

[Note: parts of this message were removed to make it a legal post.]

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
 
J

J. Cooper

Paul said:
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?
 
S

Sebastian Hungerecker

J. Cooper said:
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
 

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

Similar Threads

How can I remove the specific error message after regex validation is done? 1
regex question 3
Tasks 1
Help with dynamic regex 14
Help with regex 4
regex question 3
Short question on regex in Ruby 6
regex problem 7

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top