[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

need to split string into letters and numbers

shawn bright

5/14/2009 2:28:00 PM

[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

6 Answers

James Gray

5/14/2009 2:34:00 PM

0

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

Robert Dober

5/14/2009 2:38:00 PM

0

On Thu, May 14, 2009 at 4:34 PM, James Gray <james@grayproductions.net> wro=
te:
> 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 =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.

--
Antoine de Saint-Exup=E9ry

Jesús Gabriel y Galán

5/14/2009 2:41:00 PM

0

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.

Shajith

5/14/2009 2:44:00 PM

0

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

shawn bright

5/14/2009 4:11:00 PM

0

[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

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

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

Robert Klemme

5/14/2009 4:21:00 PM

0

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

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

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

SCNR
;-)

Kind regards

robert from "lazy typers anonymous"

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...