Forum: Ruby a simple command that splits up a string into numbers and letters

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2009-02-05 06:40
(Received via mailing list)
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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-02-05 07:05
(Received via mailing list)
On Feb 5, 2009, at 12:39 AM, shawn bright wrote:

>
> sk



irb> '34JKBY103'.scan(/\d+|\D+/)
=> ["34", "JKBY", "103"]

that looks like an easy way ;-)

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2009-02-05 07:15
(Received via mailing list)
shawn bright wrote:

> 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)?
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2009-02-05 07:34
(Received via mailing list)
sorry, how do i split on a \d+ ?
sk
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2009-02-05 07:47
(Received via mailing list)
I got it on the first try with this split!

irb> '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
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2009-02-05 07:53
(Received via mailing list)
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"]
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2009-02-05 07:55
(Received via mailing list)
shawn bright wrote:

>> > [post.]
>> >
>> > 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>
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2009-02-05 07:56
(Received via mailing list)
Tim Greer wrote:

>>>
>>> > something out there that i hav'nt found, it would probably be
> 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: http://www.ruby-doc.org/core/classes/String.html
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2009-02-05 08:03
(Received via mailing list)
way cool, thanks, all
-sk
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 Dan Diebolt (dandiebolt)
on 2009-02-05 08:19
(Received via mailing list)
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:

irb> 'a,b,c'.split(/(,)/)
=> ["a", ",", "b", ",", "c"]

irb> 'a,b,c'.split(/,/)
=> ["a", "b", "c"]
This topic is locked and can not be replied to.