Can't understand String#split's behavior

Discussion in 'Ruby' started by 冷雨, Oct 17, 2010.

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

    When I use ruby 1.8.7 I have a problem with String#split.

    It's fine:
    >>p "bAbAbAb".split(/A/)

    ["b", "b", "b", "b"]

    But it cant work:
    >>p "bAbAbAb".split(/(A)/)

    ["b", "A", "b", "A", "b", "A", "b"]

    I think that /A/ and /(A)/ are almost equivalent, but why can't I use /(A)/
    to split string?

    -Lai
     
    冷雨, Oct 17, 2010
    #1
    1. Advertising

  2. 冷雨

    botp Guest

    On Sun, Oct 17, 2010 at 8:46 AM, =E5=86=B7=E9=9B=A8 <> w=
    rote:
    > I think that /A/ and /(A)/ are almost equivalent,


    almost

    > but why can't I use /(A)/ to split string?


    as documented, you can, but w a different behaviour/result for split


    (from ruby core)
    =3D=3D=3D Implementation from String
    ---------------------------------------------------------------------------=
    ---
    str.split(pattern=3D$;, [limit]) -> anArray

    ---------------------------------------------------------------------------=
    ---

    Divides str into substrings based on a delimiter, returning an array of
    these substrings.

    If pattern is a String, then its contents are used as the
    delimiter when splitting str. If pattern is a single
    space, str is split on whitespace, with leading whitespace and runs of
    contiguous whitespace characters ignored.

    If pattern is a Regexp, str is divided where the pattern
    matches. Whenever the pattern matches a zero-length string, str is split
    into individual characters. If pattern contains groups, the
    respective matches will be returned in the array as well.
    ....
     
    botp, Oct 17, 2010
    #2
    1. Advertising

  3. Sorry for my careless.
    And thank you very match.


    On Sun, Oct 17, 2010 at 10:42 AM, botp <> wrote:

    > On Sun, Oct 17, 2010 at 8:46 AM, =E5=86=B7=E9=9B=A8 <>=

    wrote:
    > > I think that /A/ and /(A)/ are almost equivalent,

    >
    > almost
    >
    > > but why can't I use /(A)/ to split string?

    >
    > as documented, you can, but w a different behaviour/result for split
    >
    >
    > (from ruby core)
    > =3D=3D=3D Implementation from String
    >
    > -------------------------------------------------------------------------=

    -----
    > str.split(pattern=3D$;, [limit]) -> anArray
    >
    >
    > -------------------------------------------------------------------------=

    -----
    >
    > Divides str into substrings based on a delimiter, returning an array of
    > these substrings.
    >
    > If pattern is a String, then its contents are used as the
    > delimiter when splitting str. If pattern is a single
    > space, str is split on whitespace, with leading whitespace and runs of
    > contiguous whitespace characters ignored.
    >
    > If pattern is a Regexp, str is divided where the pattern
    > matches. Whenever the pattern matches a zero-length string, str is split
    > into individual characters. If pattern contains groups, the
    > respective matches will be returned in the array as well.
    > ....
    >
    >
     
    冷雨, Oct 17, 2010
    #3
    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. Replies:
    2
    Views:
    475
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    712
    Alex Martelli
    Sep 17, 2004
  3. Armen Tsirunyan
    Replies:
    50
    Views:
    1,738
  4. Sam Kong
    Replies:
    5
    Views:
    255
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    638
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page