简体   繁体   中英

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?

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])+/, ' ')

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM