String split drops the delimiter

Discussion in 'Ruby' started by basi, Dec 5, 2005.

  1. basi

    basi Guest

    Hello:

    string.split drops the delimiter. Thus:

    data = " This is a declarative sentence. And is this a question? Yes! "
    for sentence in data.split(/[.!?] /)
    puts sentence
    end

    gives

    This is a declarative sentence
    And is this a question
    Yes

    I'd like the result to retain the delimiter, thus:

    This is a declarative sentence.
    And is this a question?
    Yes!

    Any easy way to do this?
    Thanks!
    basi
     
    basi, Dec 5, 2005
    #1
    1. Advertising

  2. basi

    Jim Freeze Guest

    Use ().

    irb(main):001:0> s=3D"a,b,c,d"
    =3D> "a,b,c,d"
    irb(main):002:0> s.split(/(,)/)
    =3D> ["a", ",", "b", ",", "c", ",", "d"]


    On 12/5/05, basi <> wrote:
    > Hello:
    >
    > string.split drops the delimiter. Thus:
    >
    > data =3D " This is a declarative sentence. And is this a question? Yes! "
    > for sentence in data.split(/[.!?] /)
    > puts sentence
    > end
    >
    > gives
    >
    > This is a declarative sentence
    > And is this a question
    > Yes
    >
    > I'd like the result to retain the delimiter, thus:
    >
    > This is a declarative sentence.
    > And is this a question?
    > Yes!
    >
    > Any easy way to do this?
    > Thanks!
    > basi
    >
    >
    >



    --
    Jim Freeze
     
    Jim Freeze, Dec 5, 2005
    #2
    1. Advertising

  3. On Dec 5, 2005, at 2:12 PM, basi wrote:

    > Hello:
    >
    > string.split drops the delimiter. Thus:
    >
    > data = " This is a declarative sentence. And is this a question?
    > Yes! "
    > for sentence in data.split(/[.!?] /)
    > puts sentence
    > end
    >
    > gives
    >
    > This is a declarative sentence
    > And is this a question
    > Yes
    >
    > I'd like the result to retain the delimiter, thus:
    >
    > This is a declarative sentence.
    > And is this a question?
    > Yes!
    >
    > Any easy way to do this?


    split() can be asked to include it, though it puts it in a separate
    field:

    >> data = " This is a declarative sentence. And is this a question?

    Yes! "
    => " This is a declarative sentence. And is this a question? Yes! "
    >> data.split(/([.!?])\s+/)

    => [" This is a declarative sentence", ".", "And is this a question",
    "?", "Yes", "!"]

    Hope that helps.

    James Edward Gray II
     
    James Edward Gray II, Dec 5, 2005
    #3
  4. basi wrote:
    > Hello:
    >
    > string.split drops the delimiter. Thus:
    > I'd like the result to retain the delimiter, thus:
    >
    > Any easy way to do this?


    How about:

    puts data.scan(/\S.*?[.!?]/)
     
    Bob Showalter, Dec 5, 2005
    #4
  5. basi

    basi Guest

    Hi,
    This works for me.
    Thanks much!
    basi
     
    basi, Dec 5, 2005
    #5
  6. On 12/6/05, basi <> wrote:
    > Hi,
    > This works for me.
    > Thanks much!
    > basi


    anternatively

    data =3D " This is a declarative sentence. And is this a question? Yes! "
    data.gsub!(/(?<=3D[.?!])/, "\n")


    # -> " This is a declarative sentence.\n And is this a question?\n Yes!\n "


    --
    Simon Strandgaard
     
    Simon Strandgaard, Dec 6, 2005
    #6
  7. basi

    basi Guest

    Hi,
    A bit of a problem trying to run your code:

    irb(main):369:0> data
    => " This is a declarative sentence. And is this a question? Yes! "
    irb(main):370:0> data.gsub!(/(?<=[.?!])/, "\n")
    SyntaxError: compile error
    (irb):370: undefined (?...) sequence: /(?<=[.?!])/
    from (irb):370
    from c:/ruby/lib/ruby/1.8/drb/drb.rb:492
    irb(main):371:0>

    Also, let's modify the data a bit, thus:

    data = ' "Is this a question?" he asked. "Yes!" she exclaimed.'

    I'd like to get the sentences:

    "Is this a question?" he asked.
    "Yes!" she exclaimed.

    Rather than below, which is what I'm getting now.

    "Is this a question?
    " he asked.
    "Yes!
    " she exclaimed.

    Thanks!
    basi
     
    basi, Dec 6, 2005
    #7
  8. On 12/6/05, basi <> wrote:
    > Hi,
    > A bit of a problem trying to run your code:
    >
    > irb(main):369:0> data
    > =3D> " This is a declarative sentence. And is this a question? Yes! "
    > irb(main):370:0> data.gsub!(/(?<=3D[.?!])/, "\n")
    > SyntaxError: compile error
    > (irb):370: undefined (?...) sequence: /(?<=3D[.?!])/
    > from (irb):370
    > from c:/ruby/lib/ruby/1.8/drb/drb.rb:492
    > irb(main):371:0>
    >
    > Also, let's modify the data a bit, thus:
    >
    > data =3D ' "Is this a question?" he asked. "Yes!" she exclaimed.'
    >
    > I'd like to get the sentences:
    >
    > "Is this a question?" he asked.
    > "Yes!" she exclaimed.
    >


    data.scan(/(?:"[^"]*?"|[^.?!])*[.?!]\s?/)
    #=3D> [" \"Is this a question?\" he asked. ", "\"Yes!\" she exclaimed."]

    --
    Simon Strandgaard
     
    Simon Strandgaard, Dec 6, 2005
    #8
  9. basi

    basi Guest

    Hi,
    Thanks for supplying the last piece of "my" puzzle. It is much
    appreciated.
    basi
     
    basi, Dec 6, 2005
    #9
    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. Mark Fox

    Delimiter Split

    Mark Fox, Aug 11, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    3,342
    Chris R. Timmons
    Aug 11, 2003
  2. Kevin Spencer
    Replies:
    5
    Views:
    1,222
    =?Utf-8?B?UENL?=
    Jan 21, 2004
  3. Replies:
    9
    Views:
    374
    Paul McGuire
    Nov 16, 2006
  4. Replies:
    18
    Views:
    5,130
    Michael Jung
    Aug 11, 2013
  5. rewonka
    Replies:
    10
    Views:
    686
    M.-A. Lemburg
    Mar 19, 2009
Loading...

Share This Page