Spliting input at blank line

I’m trying to read in a file with blocks of text separated by blank
lines. How do I go about spliting the input file using the blank lines
as delimiters?

For example I havea file containing the following:

[1] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.

[2] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.

[3] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.

And I want to split it into chunks of text like this:

[1] Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.

I get how to split by newline characters but how do I split at a blank
line?

Thanks

On Sep 25, 2007, at 5:27 PM, Arfon S. wrote:

I’m trying to read in a file with blocks of text separated by blank
lines. How do I go about spliting the input file using the blank
lines
as delimiters?

If blank lines are regular:

File.foreach(fname, “\n\n”) do |block|
# … do something with block.chomp
end

Otherwise slurp the entire file and

blocks = contents.split(/\n\n+/)

– fxn

On Sep 25, 9:27 am, Arfon S. [email protected] wrote:

I’m trying to read in a file with blocks of text separated by blank
lines. How do I go about spliting the input file using the blank lines
as delimiters?

my_text.split /\n\n/

Or, if you want to allow whitespace on that ‘blank’ line:

my_text.split /\n[ \t]*\n/

great, thanks guys!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs