How to remove carriage return using Ruby?

I thought this code would work, but the regex never matches \ r \ n. I looked at the data that I am reading in the hex editor and checked that the file really has a hexadecimal D and a hexadecimal pattern.

I also tried the regular expressions / \ xD \ xA / m and / \ x0D \ x0A / m, but they also did not match.

This is my code right now:

lines2 = lines.gsub( /\r\n/m, "\n" ) if ( lines == lines2 ) print "still the same\n" else print "made the change\n" end 

In addition to the alternatives, it would be nice to know what I'm doing wrong (to make learning easier on my part). :)

+65
ruby regex
Nov 13 '08 at 18:00
source share
12 answers

What do you get when you do puts lines ? This will give you the key.

By default, File.open opens the file in text mode, so your \r\n characters will be automatically converted to \n . Perhaps the reason for lines always equal to lines2 . To prevent Ruby string parsing, use rb mode:

  C: \> copy con lala.txt
 a
 file
 with
 many
 lines
 ^ Z

 C: \> irb
 irb (main): 001: 0> text = File.open ('lala.txt'). read
 => "a \ nfile \ nwith \ nmany \ nlines \ n"
 irb (main): 002: 0> bin = File.open ('lala.txt', 'rb'). read
 => "a \ r \ nfile \ r \ nwith \ r \ nmany \ r \ nlines \ r \ n"
 irb (main): 003: 0>

But from your question and code, I see that you just need to open the file using the default modifier. You do not need any conversion and can use the shorter File.read .

+23
Nov 13 '08 at 19:03
source share

Use String # strip

Returns a copy of str with leading and trailing spaces removed.

eg

 " hello ".strip #=> "hello" "\tgoodbye\r\n".strip #=> "goodbye" 



Using gsub

 string = string.gsub(/\r/," ") string = string.gsub(/\n/," ") 
+147
Aug 17 2018-11-18T00:
source share

Usually, when I am doing the removal of \ r or \ n, I will look for both, doing something like

 lines.gsub(/\r\n?/, "\n"); 

I found that depending on how the data was saved (OS used, editor used, Jupiter's relation to Io at that time), there may or may not be a new line after the carriage return. It seems strange that you see both characters in hex mode. Hope this helps.

+33
Nov 13 '08 at 18:38
source share
 modified_string = string.gsub(/\s+/, ' ').strip 
+17
Jan 17 '12 at 8:00
source share
 lines2 = lines.split.join("\n") 
+15
Nov 13 '08 at 18:11
source share

"still the same\n".chomp
or
"still the same\n".chomp!

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-chomp

+13
Dec 01 '11 at 11:45
source share

If you use Rails, there is a squish way

"\tgoodbye\r\n".squish => "goodbye"

"\tgood \t\r\nbye\r\n".squish => "good bye"

+11
Feb 26 '16 at 13:07 on
source share

What about the following?

 irb(main):003:0> my_string = "Some text with a carriage return \r" => "Some text with a carriage return \r" irb(main):004:0> my_string.gsub(/\r/,"") => "Some text with a carriage return " irb(main):005:0> 

Or...

 irb(main):007:0> my_string = "Some text with a carriage return \r\n" => "Some text with a carriage return \r\n" irb(main):008:0> my_string.gsub(/\r\n/,"\n") => "Some text with a carriage return \n" irb(main):009:0> 
+6
Nov 13 '08 at 18:13
source share

Why not read the file in text mode and not in binary mode?

+2
Aug 17 '11 at 10:01
source share

You can use this:

 my_string.strip.gsub(/\s+/, ' ') 
+1
Jun 03 '13 at 15:41
source share
 lines.map(&:strip).join(" ") 
+1
Aug 18 '17 at 2:36 on
source share

I think your regex is almost complete - this is what I will do:

 lines2 = lines.gsub(/[\r\n]+/m, "\n") 

In the above example, I put \ r and \ n in the class (so it doesn’t matter in which order they appear) and add the qualifier "+" (so that "\ r \ n \ r \ n \ r \ n "will also match once, and all this is replaced by" \ n ")

0
Oct 05 '17 at 5:31 on
source share



All Articles