Forum: Ruby String split drops the delimiter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Acdabad15b58ba105df230e3ea02523d?d=identicon&s=25 basi_lio (Guest)
on 2005-12-05 21:14
(Received via mailing list)
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
8ecb8bb62c3283f8069a54056c7dc25f?d=identicon&s=25 jim (Guest)
on 2005-12-05 21:18
(Received via mailing list)
Use ().

irb(main):001:0> s="a,b,c,d"
=> "a,b,c,d"
irb(main):002:0> s.split(/(,)/)
=> ["a", ",", "b", ",", "c", ",", "d"]
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-05 21:22
(Received via mailing list)
On Dec 5, 2005, at 2:12 PM, basi wrote:

> gives
>
> 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
123320fdc17940dfc8e365edb48fbff2?d=identicon&s=25 bob_showalter (Guest)
on 2005-12-05 21:30
(Received via mailing list)
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.*?[.!?]/)
Acdabad15b58ba105df230e3ea02523d?d=identicon&s=25 basi_lio (Guest)
on 2005-12-06 01:27
(Received via mailing list)
Hi,
This works for me.
Thanks much!
basi
896cfc242a7762467c2a0b2af86598e5?d=identicon&s=25 neoneye (Guest)
on 2005-12-06 05:10
(Received via mailing list)
On 12/6/05, basi <basi_lio@hotmail.com> wrote:
> Hi,
> This works for me.
> Thanks much!
> basi

anternatively

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


# -> " This is a declarative sentence.\n And is this a question?\n
Yes!\n "
Acdabad15b58ba105df230e3ea02523d?d=identicon&s=25 basi_lio (Guest)
on 2005-12-06 07:40
(Received via mailing list)
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
896cfc242a7762467c2a0b2af86598e5?d=identicon&s=25 neoneye (Guest)
on 2005-12-06 08:13
(Received via mailing list)
On 12/6/05, basi <basi_lio@hotmail.com> wrote:
> 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.
>

data.scan(/(?:"[^"]*?"|[^.?!])*[.?!]\s?/)
#=> [" \"Is this a question?\" he asked. ", "\"Yes!\" she exclaimed."]
Acdabad15b58ba105df230e3ea02523d?d=identicon&s=25 basi_lio (Guest)
on 2005-12-06 09:14
(Received via mailing list)
Hi,
Thanks for supplying the last piece of "my" puzzle. It is much
appreciated.
basi
This topic is locked and can not be replied to.