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", "!"]
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.
data.scan(/(?:"[^"]*?"|[^.?!])*[.?!]\s?/)
#=> [" \"Is this a question?\" he asked. ", "\"Yes!\" she exclaimed."]
···
On 12/6/05, basi <basi_lio@hotmail.com> wrote:
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.