Need to split string into letters and numbers

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

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

···

On May 14, 2009, at 9:28 AM, shawn bright wrote:

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.

···

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

Hey,

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

···

On Thu, May 14, 2009 at 9:28 AM, shawn bright <nephish@gmail.com> wrote:

ok James so you beat me to the line, but I gotta fancy 1.9 regexen instead :wink:

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

which might help unicoders.

Cheers
Robert

···

On Thu, May 14, 2009 at 4:34 PM, James Gray <james@grayproductions.net> wrote:

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

--
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.

--
Antoine de Saint-Exupéry

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

···

On Thu, May 14, 2009 at 9:44 AM, Shajith Chacko <demerzel@gmail.com> wrote:

scan(/\d+|[A-Za-z]+/)

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

SCNR
:wink:

Kind regards

robert from "lazy typers anonymous"

···

2009/5/14 James Gray <james@grayproductions.net>:

I suggest:

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/