ruby gsub new line characters

TheLegend picture TheLegend · Sep 8, 2016 · Viewed 9.4k times · Source

I have a string with newline characters that I want to gsub out for white space.

"hello I\r\nam a test\r\n\r\nstring".gsub(/[\\r\\n]/, ' ')

something like this ^ only my regex seems to be replacing the 'r' and 'n' letters as well. the other constraint is sometimes the pattern repeats itself twice and thus would be replaced with two whitespaces in a row, although this is not preferable it is better than all the text being cut apart.

If there is a way to only select the new line characters. Or even better if there a more rubiestic way of approaching this outside of going to regex?

Answer

Wiktor Stribiżew picture Wiktor Stribiżew · Sep 8, 2016

If you have mixed consecutive line breaks that you want to replace with a single space, you may use the following regex solution:

s.gsub(/\R+/, ' ')

See the Ruby demo.

The \R matches any type of line break and + matches one or more occurrences of the quantified subpattern.

Note that in case you have to deal with an older version of Ruby, you will need to use the negated character class [\r\n] that matches either \r or \n:

.gsub(/[\r\n]+/, ' ')

or - add all possible linebreaks:

/gsub(/(?:\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029])+/, ' ')