[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: format string for array question

Robert Klemme

10/23/2007 6:55:00 AM

2007/10/22, Robert Keller <rlkeller@yahoo.com>:
> I have a great deal of data that looks like the line below
>
> "db file parallel write 8816 391 25.49"
>
>
> The code to bring this into an array had a problem due to the spaces in the test
> string "db file parallel write" I'd be happy to turn "db file parallel write" into
> db_file_parallel_write (with underscores) first then just split the string, but all
> of the regex I've tried so far has had negative effects on the rest of the string with the numbers.

Why not match directly:

irb(main):002:0> s="db file parallel write 8816 391 25.49"
=> "db file parallel write 8816 391 25.49"
irb(main):003:0> m=s.match(/^(.+\b)\s+(\d+)\s+(\d+)\s+(\d+(?:\.\d+)?)$/)
=> #<MatchData:0x7ff807bc>
irb(main):004:0> (1..4).each {|i| p m[i]}
"db file parallel write"
"8816"
"391"
"25.49"
=> 1..4

Kind regards

robert