# Splitting string into array keeping delimiters

**URL:** <https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262>\
**Category:** Ruby\
**Created:** [December 15, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262 "2007-12-15T16:16:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [December 15, 2007, 4:16pm UTC](https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262/1 "2007-12-15T16:16:54Z")

</div>

Hi, I’ve been playing Ruby for a few months now.  
Yesterday I came across an interesting problem.  
If I have this string:  
abcd1234abc123  
Now I want to separate the digit group with the non-digit group into an  
array like this [“abcd”,1234,“abc”,123]. It’s like re.split in Python.  
How can I do it in Ruby with the least lines of code possible?  
‘abcd1234abc123’.split(/\d+/) only returns [“abcd”,“abc”]  
Thank you in advance

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [December 15, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262/2 "2007-12-15T16:41:30Z")

</div>

Gary C40 wrote:

> If I have this string:  
> abcd1234abc123  
> Now I want to separate the digit group with the non-digit group into an  
> array like this [“abcd”,1234,“abc”,123]. It’s like re.split in Python.  
> How can I do it in Ruby with the least lines of code possible?  
> ‘abcd1234abc123’.split(/\d+/) only returns [“abcd”,“abc”]

If the regex has capturing groups you’ll get those in the array as well.  
‘abcd1234abc123’.split(/(\d+)/) #=\> [“abcd”, “1234”, “abc”, “123”]

If you need the numbers as integers, you could use something like:  
‘abcd1234abc123’.scan(/(\D+)(\d+)?/).map {|nd,d| [nd,d.to\_i]}  
#=\> [[“abcd”, 1234], [“abc”, 123]]  
(Maybe add flatten and compact)

or

res=  
‘abcd1234abc123’.scan(/(\D+)(\d+)?/) do  
res \<\< $1  
res \<\< $2.to\_i if $2  
end  
res #=\> [“abcd”, 1234, “abc”, 123]

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [December 15, 2007, 4:48pm UTC](https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262/3 "2007-12-15T16:48:32Z")

</div>

On Dec 15, 2007, at 4:39 PM, Sebastian H. wrote:

> If the regex has capturing groups you’ll get those in the array as  
> well.  
> ‘abcd1234abc123’.split(/(\d+)/) #=\> [“abcd”, “1234”, “abc”, “123”]
> 
> If you need the numbers as integers, you could use something like:  
> ‘abcd1234abc123’.scan(/(\D+)(\d+)?/).map {|nd,d| [nd,d.to\_i]}  
> #=\> [[“abcd”, 1234], [“abc”, 123]]  
> (Maybe add flatten and compact)

Just another one:

‘abcd1234abc123’.scan(/\D+|\d+/) # [“abcd”, “1234”, “abc”, “123”]

– fxn

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [December 15, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262/4 "2007-12-15T17:55:57Z")

</div>

On Dec 15, 8:47 am, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Python.  
> > > (Maybe add flatten and compact)
> 
> Just another one:
> 
> ‘abcd1234abc123’.scan(/\D+|\d+/) # [“abcd”, “1234”, “abc”, “123”]

And another, from 1.9:

irb(main):004:0\> str.split /(?\<=\D)(?=\d)|(?\<=\d)(?=\D)/  
=\> [“abcd”, “1234”, “abc”, “123”]

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [December 16, 2007, 6:05am UTC](https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262/5 "2007-12-16T06:05:06Z")

</div>

Wow, there sure are more than one way to solve a problem in Ruby.  
Thanks for your help, I’ll try them out 😉

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [December 16, 2007, 1:02am UTC](https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262/6 "2007-12-16T01:02:02Z")

</div>

Phrogz wrote:

> On Dec 15, 8:47 am, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> And another, from 1.9:

Yet another, after “gem install facets” and require “facets/string”:

‘abcd1234abc123’.shatter(/\d+/)

---

<div class="post-metadata">

**Author:** ![skeptic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@skeptic](https://www.ruby-forum.com/u/skeptic)\
**Post date:** [December 16, 2007, 11:52am UTC](https://www.ruby-forum.com/t/splitting-string-into-array-keeping-delimiters/123262/7 "2007-12-16T11:52:42Z")

</div>

On Dec 15, 9:16 am, Gary C40 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted viahttp://www.ruby-forum.com/.  
> And the highly esoteric version…

n = []  
s = []  
‘abcd1234abc123’.each\_byte { | x |  
if (47..57).include?(x)  
then s \<\< ; n.last \<\< x  
else n \<\< ; s.last \<\< x  
end  
}  
n = n.reject { | x | x.empty? }.map { | x |  
x.map {| y | y.chr }.join(“”).to\_i }  
s = s.reject { | x | x.empty? }.map { | x |  
x.map { | y | y.chr }.join(“”) }  
result = if n.length \> s.length  
then n.zip(s).flatten.compact  
else s.zip(n).flatten.compact  
end  
p result # =\> [“abcd”, 1234, “abc”, 123]

Regards,  
Jordan
