Split on different newlines

Right now I am doing a split in the line and assuming that the new line from user \r\n looks like this:

 string.split(/\r\n/) 

What I would like to do is split into \r\n or just \n .

So, how will the regex be split into any of these?

+49
split ruby regex newline
Jul 01 '11 at 17:14
source share
8 answers

Have you tried /\r?\n/ ? ? makes \r optional.

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

+72
Jul 01 2018-11-17T00:
source share
 # Split on \r\n or just \n string.split( /\r?\n/ ) 

Although this does not help in this question (where you need a regular expression), note that String#split does not require a regex argument. Your source code could also be string.split( "\r\n" ) .

+15
Jul 01 2018-11-17T00:
source share

Ruby has String#each_line and String#lines methods

returns enumeration: 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 have not tested it against your script, but I'm sure it will work better than manually selecting newline characters.

+13
Jul 24 '14 at 4:15
source share

The rotation operator in Ruby Regexp is the same as in standard regular expressions: |

So the obvious solution would be

 /\r\n|\n/ 

which coincides with

 /\r?\n/ 

i.e. optional \r followed by mandatory \n .

+3
Jul 01 '11 at 17:18
source share

Maybe just split "\ n" and delete "\ r" if it exists?

+1
Jul 01 2018-11-17T00:
source share

Are you reading from a file or from standard in?

If you are reading from a file and the file is in text mode and not in binary mode, or you are reading the standard version, you will not have to deal with \r\n - it will just look like \n .

 C:\Documents and Settings\username>irb irb(main):001:0> gets foo => "foo\n" 
+1
Jul 03 '11 at 23:10
source share

Another option is to use String # chomp , which also by itself processes newlines.

You can accomplish what you need with something like:

 lines = string.lines.map(&:chomp) 

Or if you are dealing with something big enough to use memory:

 <string|io>.each_line do |line| line.chomp! # do work.. end 

Performance is not always the most important thing when solving this kind of problem, but it is worth noting that the chomp solution is also slightly faster than using a regular expression.

On my machine (i7, ruby ​​2.1.9):

 Warming up -------------------------------------- map/chomp 14.715ki/100ms split custom regex 12.383ki/100ms Calculating ------------------------------------- map/chomp 158.590k (Β± 4.4%) i/s - 794.610k in 5.020908s split custom regex 128.722k (Β± 5.1%) i/s - 643.916k in 5.016150s 
0
Aug 16 '16 at 23:34
source share
 \n is for unix \r is for mac \r\n is for windows format 

To be safe for operating systems. I would do / \ r? \ N | \ r \ n? /

 "1\r2\n3\r\n4\n\n5\r\r6\r\n\r\n7".split(/\r?\n|\r\n?/) => ["1", "2", "3", "4", "", "5", "", "6", "", "7"] 
0
Apr 11 '17 at 17:31 on
source share



All Articles