Forum: Ruby need to split string into letters and numbers

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-05-14 16:30
(Received via mailing list)
Hey all

I need to be able to split a string into lumps of numbers and letters.
so for example if st = "JJ542JQ83" would become ['JJ', '542','JQ',"83"]

i have been using st.split(/([a-zA-Z]+)(?=[0-9])/) , this does pretty
well
most of the time,
but also fails sometimes to do what i need it to . ( like for 202GP)

any help would be much appreciated, thanks
sk
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-05-14 16:35
(Received via mailing list)
On May 14, 2009, at 9:28 AM, shawn bright wrote:

> Hey all

Hello.

> I need to be able to split a string into lumps of numbers and letters.
> so for example if st = "JJ542JQ83" would become ['JJ',
> '542','JQ',"83"]
>
> i have been using st.split(/([a-zA-Z]+)(?=[0-9])/) , this does
> pretty well
> most of the time,
> but also fails sometimes to do what i need it to . ( like for 202GP)

I suggest:

   str.scan(/\d+|[a-zA-Z]+/)

Hope that helps.

James Edward Gray II
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2009-05-14 16:38
(Received via mailing list)
On Thu, May 14, 2009 at 4:34 PM, James Gray <james@grayproductions.net>
wrote:
>> most of the time,
>
ok James so you beat me to the line, but I gotta fancy 1.9 regexen
instead ;)

str.scan /\p{Alpha}+|\p{Digit}+/u

which might help unicoders.

Cheers
Robert




--
Si tu veux construire un bateau ...
Ne rassemble pas des hommes pour aller chercher du bois, préparer des
outils, répartir les tâches, alléger le travail… mais enseigne aux
gens la nostalgie de l’infini de la mer.

If you want to build a ship, don’t herd people together to collect
wood and don’t assign them tasks and work, but rather teach them to
long for the endless immensity of the sea.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-05-14 16:42
(Received via mailing list)
On Thu, May 14, 2009 at 4:28 PM, shawn bright <nephish@gmail.com> wrote:
> Hey all
>
> I need to be able to split a string into lumps of numbers and letters.
> so for example if st = "JJ542JQ83" would become ['JJ', '542','JQ',"83"]
>
> i have been using st.split(/([a-zA-Z]+)(?=[0-9])/) , this does pretty well
> most of the time,
> but also fails sometimes to do what i need it to . ( like for 202GP)
>
> any help would be much appreciated, thanks

Try scan:

irb(main):001:0> s = "JJ234AB2"
=> "JJ234AB2"
irb(main):004:0> s.scan(/[a-zA-Z]+|[0-9]+/)
=> ["JJ", "234", "AB", "2"]
irb(main):005:0> s = "202GP"
=> "202GP"
irb(main):006:0> s.scan(/[a-zA-Z]+|[0-9]+/)
=> ["202", "GP"]

Jesus.
F0e2e7181221d362a226abe48d46e0e0?d=identicon&s=25 Shajith Chacko (Guest)
on 2009-05-14 16:44
(Received via mailing list)
Hey,

On Thu, May 14, 2009 at 9:28 AM, shawn bright <nephish@gmail.com> wrote:
> I need to be able to split a string into lumps of numbers and letters.
> so for example if st = "JJ542JQ83" would become ['JJ', '542','JQ',"83"]
>
> i have been using st.split(/([a-zA-Z]+)(?=[0-9])/) , this does pretty well
> most of the time,
> but also fails sometimes to do what i need it to . ( like for 202GP)

You could use String#scan for this. Eg:

irb(main):019:0> st = "JJ542JQ83"
=> "JJ542JQ83"
irb(main):020:0> st.scan(/\d+|[A-Za-z]+/)
=> ["JJ", "542", "JQ", "83"]
irb(main):021:0> st = "202GP"
=> "202GP"
irb(main):022:0> st.scan(/\d+|[A-Za-z]+/)
=> ["202", "GP"]
irb(main):023:0>

Does that work?

Shajith
500e92cfb666d6757c317a5df7c7e28d?d=identicon&s=25 Shawn Bright (nephish)
on 2009-05-14 18:12
(Received via mailing list)
ok, i  feel like i am at a party as the only one that does not speak a
language.
Thanks for your tips, all. I need to learn reg ex.

thanks again.
shawn
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-05-14 18:22
(Received via mailing list)
2009/5/14 James Gray <james@grayproductions.net>:

> I suggest:
>
>  str.scan(/\d+|[a-zA-Z]+/)

str.scan(/\d+|[a-z]+/i)

SCNR
;-)

Kind regards

robert from "lazy typers anonymous"
This topic is locked and can not be replied to.