A simple command that splits up a string into numbers and letters

Hey all,

i am looking for an easy way to split a string into letters and numbers.
so if i had a string '34JKBY103' i could get ['34', 'JKBY', '103']

I could write up something, but thought that if there was already something
out there that i hav'nt found, it would probably be cleaner.
thanks

sk

=> ["34", "JKBY", "103"]

that looks like an easy way :wink:

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Feb 5, 2009, at 12:39 AM, shawn bright wrote:

Hey all,

i am looking for an easy way to split a string into letters and numbers.
so if i had a string '34JKBY103' i could get ['34', 'JKBY', '103']

I could write up something, but thought that if there was already something
out there that i hav'nt found, it would probably be cleaner.
thanks

sk

'34JKBY103'.scan(/\d+|\D+/)

shawn bright wrote:

[Note: parts of this message were removed to make it a legal post.]

Hey all,

i am looking for an easy way to split a string into letters and
numbers. so if i had a string '34JKBY103' i could get ['34', 'JKBY',
'103']

I could write up something, but thought that if there was already
something out there that i hav'nt found, it would probably be cleaner.
thanks

sk

I don't know if there's anything out there now (there might be), but it
seems pretty simple. Just split on either \d+ or \D+, depending
(unless I'm missing something in your requirement)?

···

--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

I got it on the first try with this split!

'34JKBY103dfd878dsf78s78s'.split(/([a-zA-Z]+)(?=[0-9])/)

=> ["34", "JKBY", "103", "dfd", "878", "dsf", "78", "s", "78s"]

the first set of parens is capturing while the second set of parents is a zero width positive look ahead

sorry, how do i split on a \d+ ?
sk

···

On Thu, Feb 5, 2009 at 12:14 AM, Tim Greer <tim@burlyhost.com> wrote:

shawn bright wrote:

> [Note: parts of this message were removed to make it a legal post.]
>
> Hey all,
>
> i am looking for an easy way to split a string into letters and
> numbers. so if i had a string '34JKBY103' i could get ['34', 'JKBY',
> '103']
>
> I could write up something, but thought that if there was already
> something out there that i hav'nt found, it would probably be cleaner.
> thanks
>
> sk

I don't know if there's anything out there now (there might be), but it
seems pretty simple. Just split on either \d+ or \D+, depending
(unless I'm missing something in your requirement)?
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

way cool, thanks, all
-sk

···

On Thu, Feb 5, 2009 at 12:46 AM, DanDiebolt.exe <dandiebolt@yahoo.com>wrote:

I got it on the first try with this split!

> '34JKBY103dfd878dsf78s78s'.split(/([a-zA-Z]+)(?=[0-9])/)
=> ["34", "JKBY", "103", "dfd", "878", "dsf", "78", "s", "78s"]

the first set of parens is capturing while the second set of parents is a
zero width positive look ahead

This is pretty close to what you want except for the first null

'34JKBY103dfd878dsf78s78s'.split(/(\d+)/)

=> ["", "34", "JKBY", "103", "dfd", "878", "dsf", "78", "s", "78", "s"]

shawn bright wrote:

[Note: parts of this message were removed to make it a legal post.]

sorry, how do i split on a \d+ ?
sk

shawn bright wrote:

> [Note: parts of this message were removed to make it a legal
> [post.]
>
> Hey all,
>
> i am looking for an easy way to split a string into letters and
> numbers. so if i had a string '34JKBY103' i could get ['34',
> 'JKBY', '103']
>
> I could write up something, but thought that if there was already
> something out there that i hav'nt found, it would probably be
> cleaner. thanks
>
> sk

I don't know if there's anything out there now (there might be), but
it
seems pretty simple. Just split on either \d+ or \D+, depending
(unless I'm missing something in your requirement)?

<please don't quote signatures>

Someone posted this already, but:

irb(main):010:0> s = "34JKBY103"
=> "34JKBY103"
irb(main):011:0> s.scan(/\d+|\D+/)
=> ["34", "JKBY", "103"]
irb(main):012:0>

···

On Thu, Feb 5, 2009 at 12:14 AM, Tim Greer <tim@burlyhost.com> wrote:

--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

I sort of got hung up on doing this with split. Using scan might be easier than split. However it is worth noting that when you are splitting on a pattern you can keep what the pattern matches in the results array by capturing with a set of ()s. Compare these two statements:

'a,b,c'.split(/(,)/)

=> ["a", ",", "b", ",", "c"]

'a,b,c'.split(/,/)

=> ["a", "b", "c"]

Tim Greer wrote:

shawn bright wrote:

[Note: parts of this message were removed to make it a legal post.]

sorry, how do i split on a \d+ ?
sk

shawn bright wrote:

> [Note: parts of this message were removed to make it a legal
> [post.]
>
> Hey all,
>
> i am looking for an easy way to split a string into letters and
> numbers. so if i had a string '34JKBY103' i could get ['34',
> 'JKBY', '103']
>
> I could write up something, but thought that if there was already
> something out there that i hav'nt found, it would probably be
> cleaner. thanks
>
> sk

I don't know if there's anything out there now (there might be), but
it
seems pretty simple. Just split on either \d+ or \D+, depending
(unless I'm missing something in your requirement)?

<please don't quote signatures>

Someone posted this already, but:

irb(main):010:0> s = "34JKBY103"
=> "34JKBY103"
irb(main):011:0> s.scan(/\d+|\D+/)
=> ["34", "JKBY", "103"]
irb(main):012:0>

Also, remember, there are several ways to do this. You can use an
actual split() function with a regular expression.

See: class String - RDoc Documentation

···

On Thu, Feb 5, 2009 at 12:14 AM, Tim Greer <tim@burlyhost.com> wrote:

--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!