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?
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?
Another option is to use String#chomp, which also handles newlines intelligently by itself.
You can accomplish what you are after with something like:
Or if you are dealing with something large enough that memory use is a concern:
Performance isn't always the most important thing when solving this kind of problem, but it is worth noting the chomp solution is also a bit faster than using a regex.
On my machine (i7, ruby 2.1.9):
Although it doesn't help with this question (where you do need a regex), note that
String#split
does not require a regex argument. Your original code could also have beenstring.split( "\r\n" )
.Did you try
/\r?\n/
? The?
makes the\r
optional.Example usage: http://rubular.com/r/1ZuihD0YfF
The alternation operator in Ruby
Regexp
is the same as in standard regular expressions:|
So, the obvious solution would be
which is the same as
i.e. an optional
\r
followed by a mandatory\n
.Are you reading from a file, or from standard in?If you're reading from a file, and the file is in text mode, rather than binary mode, or you're reading from standard in, you won't have to deal with
\r\n
- it'll just look like\n
.Ruby has the methods
String#each_line
andString#lines
returns an enum: http://www.ruby-doc.org/core-1.9.3/String.html#method-i-each_line
returns an array: http://www.ruby-doc.org/core-2.1.2/String.html#method-i-lines
I didn't test it against your scenario but I bet it will work better than manually choosing the newline chars.