convert string to array with delimiter

Discussion in 'Ruby' started by Valentino Lun, Nov 5, 2008.

  1. Dear all

    I have the following string want to convert to arry

    str="/dev/lis_home_712 2097152 1052656 1044496 51%
    /appl/lis/home2\n"

    I use str.scan(/\w+/), which give the following result
    ["dev", "lis_home_712", "2097152", "1052656", "1044496", "51", "appl",
    "lis", "home2"]

    However, I expect the result like this
    ["dev/lis_home_712", "2097152", "1052656", "1044496", "51%",
    "/appl/lis/home2"]

    Please advise on how to do it. Thank you.
    --
    Posted via http://www.ruby-forum.com/.
    Valentino Lun, Nov 5, 2008
    #1
    1. Advertising

  2. 2008/11/5 Valentino Lun <>:
    > I use str.scan(/\w+/), which give the following result


    str.split( ' ' )

    Farrel
    --
    Aimred - Ruby Development and Consulting
    http://www.aimred.com
    Farrel Lifson, Nov 5, 2008
    #2
    1. Advertising

  3. RnJvbTogVmFsZW50aW5vIEx1biBbbWFpbHRvOnN1bXdvQHlhaG9vLmNvbV0gDQojIEhvd2V2ZXIs
    IEkgZXhwZWN0IHRoZSByZXN1bHQgbGlrZSB0aGlzDQojIFsiZGV2L2xpc19ob21lXzcxMiIsICIy
    MDk3MTUyIiwgIjEwNTI2NTYiLCAiMTA0NDQ5NiIsICI1MSUiLA0KIyAiL2FwcGwvbGlzL2hvbWUy
    Il0NCiMgDQojIFBsZWFzZSBhZHZpc2Ugb24gaG93IHRvIGRvIGl0LiBUaGFuayB5b3UuDQoNCnJl
    YWQgb24gc3RyaW5nIGZ4bnMsIHFyaSBTdHJpbmcjLiANCmhlcmUgaW4gdGhpcyBjYXNlIHlvdSBt
    YXkgdXNlIFN0cmluZyNzcGxpdA0KDQp0cnkgZmlyc3QsIA0KDQogIHN0ci5zcGxpdA0KDQoobm90
    ZSB3L291dCBhbiBhcmcpDQo=
    Peña, Botp, Nov 5, 2008
    #3
  4. Valentino Lun

    Heesob Park Guest

    2008/11/5 Valentino Lun <>:
    > Dear all
    >
    > I have the following string want to convert to arry
    >
    > str="/dev/lis_home_712 2097152 1052656 1044496 51%
    > /appl/lis/home2\n"
    >
    > I use str.scan(/\w+/), which give the following result
    > ["dev", "lis_home_712", "2097152", "1052656", "1044496", "51", "appl",
    > "lis", "home2"]
    >
    > However, I expect the result like this
    > ["dev/lis_home_712", "2097152", "1052656", "1044496", "51%",
    > "/appl/lis/home2"]
    >
    > Please advise on how to do it. Thank you.


    str.scan(/\S+/)
    or
    str.split(/\s+/)
    or
    str.split

    Regards,

    Park Heesob
    Heesob Park, Nov 5, 2008
    #4
    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. Pedro Rocha
    Replies:
    8
    Views:
    15,365
    Boudewijn Dijkstra
    Dec 8, 2004
  2. gbattine
    Replies:
    0
    Views:
    523
    gbattine
    Jun 19, 2006
  3. Prasanth
    Replies:
    4
    Views:
    400
    Prasanth
    Nov 22, 2008
  4. padmagvs
    Replies:
    1
    Views:
    430
    cuffJ
    Aug 16, 2010
  5. Replies:
    2
    Views:
    112
Loading...

Share This Page