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? -- Posted via http://www.ruby-forum.com/.
Mark Van Holstyn
2006-Jul-05 05:16 UTC
[Rails] splitting a story with double-spaced para''s
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 On 7/5/06, sean colquhoun <seancolquhoun@gk-a.com> wrote:> > 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Mark Van Holstyn mvette13@gmail.com http://lotswholetime.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060705/f5c79f1c/attachment.html
sean colquhoun
2006-Jul-05 05:56 UTC
[Rails] Re: splitting a story with double-spaced para''s
Thanks Mark! Looks like you came through for me again. The /\n\s*\n/ worked. What does \s mean? Mark Van Holstyn 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-- Posted via http://www.ruby-forum.com/.
Mark Van Holstyn
2006-Jul-05 06:07 UTC
[Rails] Re: splitting a story with double-spaced para''s
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 On 7/5/06, sean colquhoun <seancolquhoun@gk-a.com> wrote:> > Thanks Mark! Looks like you came through for me again. The /\n\s*\n/ > worked. > What does \s mean? > > Mark Van Holstyn 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 > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Mark Van Holstyn mvette13@gmail.com http://lotswholetime.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060705/37d61bd7/attachment.html