# What's the best way to split this kind of string?

**URL:** <https://www.ruby-forum.com/t/whats-the-best-way-to-split-this-kind-of-string/80220>\
**Category:** Ruby\
**Created:** [March 13, 2006, 9:01am UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-split-this-kind-of-string/80220 "2006-03-13T09:01:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [March 13, 2006, 9:01am UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-split-this-kind-of-string/80220/1 "2006-03-13T09:01:23Z")

</div>

Hi!

I’m trying to make a routine to make a Morris Number Sequence.  
([http://www.ocf.berkeley.edu/~stoll/number\_sequence.html](http://www.ocf.berkeley.edu/~stoll/number_sequence.html))  
I need to split a string in the following way, for example.

“111223133” =\> [“111”, “22”, “3”, “1”, “33”]

I can loop thru the string by each character, comparing with the  
previous character.  
But is there a quick and easy way?  
Probably a regular expression?

Thanks.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [March 13, 2006, 9:17am UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-split-this-kind-of-string/80220/2 "2006-03-13T09:17:41Z")

</div>

Sam K. wrote:

> Hi!
> 
> I’m trying to make a routine to make a Morris Number Sequence.  
> ([http://www.ocf.berkeley.edu/~stoll/number\_sequence.html](http://www.ocf.berkeley.edu/~stoll/number_sequence.html))  
> I need to split a string in the following way, for example.
> 
> “111223133” =\> [“111”, “22”, “3”, “1”, “33”]
> 
> I can loop thru the string by each character, comparing with the  
> previous character.  
> But is there a quick and easy way?  
> Probably a regular expression?

This seems to work:

“111223133”.scan(/1+|2+|3+|4+|5+|6+|7+|8+|9+|0+/)  
=\> [“111”, “22”, “3”, “1”, “33”]

–  
– Jim W.

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [March 13, 2006, 9:25am UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-split-this-kind-of-string/80220/3 "2006-03-13T09:25:38Z")

</div>

On Mon, 2006-03-13 at 16:58 +0900, Sam K. wrote:

> But is there a quick and easy way?  
> Probably a regular expression?

Maybe something like:

```
s.scan(/(\d)(\1*)/).map! { |e| e.join }
# => ["111", "22", "3", "1", "33"]

```

or:

```
s.scan(/0+|1+|2+|3+|4+|5+|6+|7+|8+|9+/)
# => ["111", "22", "3", "1", "33"]
```

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [March 13, 2006, 11:00am UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-split-this-kind-of-string/80220/4 "2006-03-13T11:00:11Z")

</div>

Sam K. wrote:

> But is there a quick and easy way?  
> Probably a regular expression?
> 
> Thanks.
> 
> Sam

“111223133”.scan(/(.)(\1\*)/).map{|x|x.join}

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [March 13, 2006, 12:22pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-split-this-kind-of-string/80220/5 "2006-03-13T12:22:34Z")

</div>

On 3/13/06, Ross B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Maybe something like:
> 
> ```
> s.scan(/(\d)(\1*)/).map! { |e| e.join }
> # => ["111", "22", "3", "1", "33"]
> 
> ```

Hmm… Clever

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [March 13, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/whats-the-best-way-to-split-this-kind-of-string/80220/6 "2006-03-13T18:50:02Z")

</div>

On Mon, 13 Mar 2006 17:23:56 +0900, Ross B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:  
[snip]

> Maybe something like:
> 
> s.scan(/(\d)(\1\*)/).map! { |e| e.join }
> 
> # =\> [“111”, “22”, “3”, “1”, “33”]
> 
> or:
> 
> s.scan(/0+|1+|2+|3+|4+|5+|6+|7+|8+|9+/)
> 
> # =\> [“111”, “22”, “3”, “1”, “33”]

Or:

s.scan(/((.)\2\*)/).transpose[0]

Boy, are my fingers glad to save those keystrokes 🙂

andrew
