need to split string into letters and numbers

S

shawn bright

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

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
 
J

James Gray

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
 
R

Robert Dober

Hey all
Hello.

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

i have been using st.split(/([a-zA-Z]+)(?=3D[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:

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

Hope that helps.

James Edward Gray II
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




--=20
Si tu veux construire un bateau ...
Ne rassemble pas des hommes pour aller chercher du bois, pr=E9parer des
outils, r=E9partir les t=E2ches, all=E9ger le travail=85 mais enseigne aux
gens la nostalgie de l=92infini de la mer.

If you want to build a ship, don=92t herd people together to collect
wood and don=92t assign them tasks and work, but rather teach them to
long for the endless immensity of the sea.
 
J

Jesús Gabriel y Galán

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

Shajith Chacko

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
 
S

shawn bright

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top