Splitting a story with double-spaced para's

Quick question - if i want to separate a long string of text, separated
by single carriage returns, into an array of paragraphs, I can use the
following code:

paragraphs = article.content.split("\n")

The “\n” means a carriage return, as far as I can tell.

If I wanted to take text, that had been written using the more common
double carriage returns, into an array of paragraphs in the same
fashion, then logically I should be able to do this:

paragraphs = article.content.split("\n\n")

Right?

However, it isn’t working. I’ve tried split("\n \n"), split("\n/\n"),
split("\n" “\n”), all to no avail. Could somebody kindly point me in the
right direction?

You might be seeing a problem between the different type of new line
characters. “\n” is a line feed, and “\r” is a carriage return. unix
systems
use “\n” for new lines, windows uses “\r\n”. Another possibility is that
there is some other white space between the two new lines. If so maybe
something like split( /\n\s*\n/ ). Also, I would recommend using /\n/
rather
than “\n” to be able to match on a regex, rather than just a string.
hope
that helps.

mark

Thanks Mark! Looks like you came through for me again. The /\n\s*\n/
worked.
What does \s mean?

Mark Van H. wrote:

You might be seeing a problem between the different type of new line
characters. “\n” is a line feed, and “\r” is a carriage return. unix
systems
use “\n” for new lines, windows uses “\r\n”. Another possibility is that
there is some other white space between the two new lines. If so maybe
something like split( /\n\s*\n/ ). Also, I would recommend using /\n/
rather
than “\n” to be able to match on a regex, rather than just a string.
hope
that helps.

mark

In a regex \s denotes any whitespace character ( could be a space, a
tab, a
newline ). The * denotes that there could be zero, or could be any
number of
them in the match. Glad to help.

mark

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