Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby gsub new line characters

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?

like image 747
TheLegend Avatar asked Sep 08 '16 14:09

TheLegend


2 Answers

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])+/, ' ')
like image 105
Wiktor Stribiżew Avatar answered Sep 24 '22 11:09

Wiktor Stribiżew


This should work for your test case:

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

If you don't want successive \r\n characters to result in duplicate spaces you can use this instead:

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

(Note the addition of the + after the character class.)

As Wiktor mentioned, you're using \\ in your regex, which inside the regex literal /.../ actually escapes a backslash, meaning you're matching a literal backslash \, r, or n as part of your expression. Escaping characters works differently in regex literals, since \ is used so much, it makes no sense to have a special escape for it (as opposed to regular strings, which is a whole different animal).

like image 39
HerbCSO Avatar answered Sep 23 '22 11:09

HerbCSO