getting the results of a simple regex

R

Raimon Fs

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=> nil


Playing with the 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.
 
R

Robert Klemme

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
=> nil


Playing with the 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 ?

You do not use String#scan. :)

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

Kind regards

robert
 
R

Raimon Fs

Robert said:
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.
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top