need to split string into letters and numbers

Discussion in 'Ruby' started by shawn bright, May 14, 2009.

  1. shawn bright

    shawn bright Guest

    [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
    shawn bright, May 14, 2009
    #1
    1. Advertising

  2. shawn bright

    James Gray Guest

    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
    James Gray, May 14, 2009
    #2
    1. Advertising

  3. shawn bright

    Robert Dober Guest

    On Thu, May 14, 2009 at 4:34 PM, James Gray <> 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
    Robert Dober, May 14, 2009
    #3
  4. On Thu, May 14, 2009 at 4:28 PM, shawn bright <> 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.
    Jesús Gabriel y Galán, May 14, 2009
    #4
  5. Hey,

    On Thu, May 14, 2009 at 9:28 AM, shawn bright <> 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
    Shajith Chacko, May 14, 2009
    #5
  6. shawn bright

    shawn bright Guest

    [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 <> wrote:

    > scan(/\d+|[A-Za-z]+/)
    >
    shawn bright, May 14, 2009
    #6
  7. 2009/5/14 James Gray <>:

    > 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.rubybestpractices.com/
    Robert Klemme, May 14, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Merrigan
    Replies:
    4
    Views:
    558
    Chris
    Dec 14, 2007
  2. Jed
    Replies:
    7
    Views:
    428
    Terry Reedy
    Aug 26, 2010
  3. shawn bright
    Replies:
    6
    Views:
    93
    shawn bright
    Feb 5, 2009
  4. Venugopal
    Replies:
    11
    Views:
    1,496
    Tassilo v. Parseval
    Nov 5, 2003
  5. Anees
    Replies:
    14
    Views:
    159
Loading...

Share This Page