Shpigford Shpigford - 3 months ago 6
Ruby Question

Split on different newlines

Right now I'm doing a

split
on a string and assuming that the newline from the user is
\r\n
like so:

string.split(/\r\n/)


What I'd like to do is split on either
\r\n
or just
\n
.

So how what would the regex be to split on either of those?

Answer

Did you try /\r?\n/ ? The ? makes the \r optional.

Example usage: http://rubular.com/r/1ZuihD0YfF

Comments