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

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

On Thu, May 14, 2009 at 4:34 PM, James G. [email protected]
wrote:

most of the time,

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


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.

Hey,

On Thu, May 14, 2009 at 9:28 AM, shawn bright [email protected] 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

On Thu, May 14, 2009 at 4:28 PM, shawn bright [email protected] 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.

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

2009/5/14 James G. [email protected]:

I suggest:

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

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

SCNR
:wink:

Kind regards

robert from “lazy typers anonymous”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs