# Regex question

**URL:** <https://www.ruby-forum.com/t/regex-question/129705>\
**Category:** Ruby\
**Created:** [February 20, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/regex-question/129705 "2008-02-20T21:15:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [February 20, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/regex-question/129705/1 "2008-02-20T21:15:26Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [February 20, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/regex-question/129705/2 "2008-02-20T21:26:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [February 20, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/regex-question/129705/3 "2008-02-20T21:41:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [February 21, 2008, 5:25am UTC](https://www.ruby-forum.com/t/regex-question/129705/4 "2008-02-21T05:25:52Z")

</div>

> Make it non-greedy: (.\*?)
> 
> HTH,  
> Sebastian

Life saver. Thanks!

---

<div class="post-metadata">

**Author:** ![nefigah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e47774/32.png) [@nefigah](https://www.ruby-forum.com/u/nefigah)\
**Post date:** [February 20, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/regex-question/129705/5 "2008-02-20T21:50:15Z")

</div>

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
