# Format string for array question

**URL:** <https://www.ruby-forum.com/t/format-string-for-array-question/117985>\
**Category:** Ruby\
**Created:** [October 22, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/format-string-for-array-question/117985 "2007-10-22T23:45:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 22, 2007, 11:45pm UTC](https://www.ruby-forum.com/t/format-string-for-array-question/117985/1 "2007-10-22T23:45:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 23, 2007, 12:07am UTC](https://www.ruby-forum.com/t/format-string-for-array-question/117985/2 "2007-10-23T00:07:22Z")

</div>

Robert K. wrote:

> numbers.
> 
> * * *
> 
> Robert  
> removed\_email\_address@domain.invalid

[rh-30-x86.irvspxl08:2182]gbystrit\> irb  
irb(main):001:0\> s = “db file parallel write 8816 391  
25.49”  
=\> “db file parallel write 8816 391 25.49”  
irb(main):002:0\> s.split(%r{(?:\s+)([\d.]+)}).reject {|s| s.empty?}  
=\> [“db file parallel write”, “8816”, “391”, “25.49”]  
irb(main):003:0\>

=Gennady.

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 23, 2007, 11:40am UTC](https://www.ruby-forum.com/t/format-string-for-array-question/117985/3 "2007-10-23T11:40:26Z")

</div>

2007/10/22, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [October 24, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/format-string-for-array-question/117985/4 "2007-10-24T14:01:39Z")

</div>

Or you could use “zero-width positive lookahead” /(?=)/ with -? for  
possible negative numbers:

s = “db file parallel -write 8816 391 -25.49”  
p s.split(/\s+(?=-?\d)/)

#=\> [“db file parallel -write”, “8816”, “391”, “-25.49”]
