# Getting the results of a simple regex

**URL:** <https://www.ruby-forum.com/t/getting-the-results-of-a-simple-regex/164860>\
**Category:** Ruby\
**Created:** [April 6, 2009, 7:13pm UTC](https://www.ruby-forum.com/t/getting-the-results-of-a-simple-regex/164860 "2009-04-06T19:13:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [April 6, 2009, 7:13pm UTC](https://www.ruby-forum.com/t/getting-the-results-of-a-simple-regex/164860/1 "2009-04-06T19:13:14Z")

</div>

Hello,

I’m playing for my very first time with Regex …

I want to split some numbers, and get their results:

Given this string:

18,04 2,89 20,93 2,71 18,22

I want to get every number before a comma, and two digits later, and all  
the groups.

I think it should work this way but I can only get the first result …

/(\d+,\d{2})/ =~ “18,04 2,89 20,93 2,71 18,22”  
=\> 0

> > $1  
> > =\> “18,04”  
> > $2  
> > =\> nil  
> > $3  
> > =\> nil  
> > $4  
> > =\> nil  
> > $5  
> > =\> nil

Playing with the [http://www.rubular.com/](http://www.rubular.com/) I can get the result that I  
want …

I also tried the Regexp.last\_match but I’m getting the same, only the  
first value …

what I’m doing wrong ?

thanks!

r.

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [April 6, 2009, 8:30pm UTC](https://www.ruby-forum.com/t/getting-the-results-of-a-simple-regex/164860/2 "2009-04-06T20:30:37Z")

</div>

On 06.04.2009 19:12, Raimon Fs wrote:

> > > $2  
> > > want …
> 
> I also tried the Regexp.last\_match but I’m getting the same, only the  
> first value …
> 
> what I’m doing wrong ?

You do not use String#scan. 🙂

Btw, you do not need the grouping as you want the complete match anyway.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [April 6, 2009, 8:39pm UTC](https://www.ruby-forum.com/t/getting-the-results-of-a-simple-regex/164860/3 "2009-04-06T20:39:28Z")

</div>

Robert K. wrote:

> On 06.04.2009 19:12, Raimon Fs wrote:
> 
> > > > $2  
> > > > want …
> > 
> > I also tried the Regexp.last\_match but I’m getting the same, only the  
> > first value …
> > 
> > what I’m doing wrong ?
> 
> You do not use String#scan. 🙂

=\> var=“18,04 2,89 20,93 2,71 18,22”

> > a=var.scan(/\d+,\d{2}/)  
> > =\> [“18,04”, “2,89”, “20,93”, “2,71”, “18,22”]
> 
> > a[0]  
> > =\> “18,04”
> 
> > a[1]  
> > =\> “2,89”
> 
> > a[2]  
> > =\> “20,93”
> 
> > a[3]  
> > =\> “2,71”
> 
> > a[4]  
> > =\> “18,22”

🙂

thanks …

> Btw, you do not need the grouping as you want the complete match anyway.

I was grouping with () because I was using:

```
  line =~ 

```

%r{(\d+,\d{2})(\s)(\d+,\d{2})(\s)(\d+,\d{2})(\s)(\d+,\d{2})(\s)(\d+,\d{2})}

and then $1, $2, $3, $4, …

thanks for your help!

regards,

r.
