Ruby Which regular expression pattern to replace \n\n to \n string - ruby-on-rails

I have a string
Java \n\n c# \n\n c/c++
i need to replace it becomes
Java \n c# \n c/c++
use regular expression in Ruby String
Thanks

Use squeeze function of String class
[18] pry(main)> "Java \n\n c# \n\n c/c++".squeeze("\n")
=> "Java \n c# \n c/c++"
However, it returns a new string where runs of the same character that occur in this set are replaced by a single character, So
[18] pry(main)> "Java \n\n\n\n\n c# \n\n\n\n\n c/c++".squeeze("\n")
=> "Java \n c# \n c/c++"

You probably want to eliminate triple and so forth occurencies as well. In such a case the best option is to use the match counter:
# ⇓⇓⇓⇓
'Java \n\n c# \n\n c/c++'.gsub /\\n{1,}/, '\n'
In this particular case, “one or more” has s syntactic sugar for it:
# ⇓
'Java \n\n c# \n\n c/c++'.gsub /\\n+/, '\n'
If you are using Ruby2, there is \R match to match any combination of \r and \n.
To eliminate exactly two occurencies, one might use:
# ⇓⇓⇓
'Java \n\n c# \n\n c/c++'.gsub /\\n{2}/, '\n'
And, finally, there is a function to remove multiple occurencies of \n from the string using named matches and backreferences:
def singlify s
s.gsub /(?<sym>\\n)\g<sym>+/, '\k<sym>'
end
singlify 'Java \n\n c# \n\n c/c++'
# Java \n c# \n c/c++'

'Java \n\n c# \n\n c/c++'.gsub! /\\n\\n/, '\n'

Related

How would I put “\” in a string without the apostrophe in the front of it being cancelled out?

For example, if I were to do this:
print(“\”)
It would say: `unfinished string near: ‘“”’
instead of my expected output of: ‘\’
How would I print this? I have searched on google yet still have yet to find an answer.
The backslash (\) is escaping the following character, being the double quote ("), causing the string to be unfinished.
To include an actual backslash in your string, you escape it with another backslash:
print("\\")
From Lua 5.4 Reference Manual, §3.1 (emphasis mine):
A short literal string can be delimited by matching single or double quotes, and can contain the following C-like escape sequences: '\a' (bell), '\b' (backspace), '\f' (form feed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\v' (vertical tab), '\\' (backslash), '"' (quotation mark [double quote]), and ''' (apostrophe [single quote]). [...]

Replace \\n \\r \\t posted in rails

I have a inout text field where user can copy paste data, I want to replace \r \n \t but when the data is posted these characters are escaped.
So a string entered by user for example hello \r\n\t world is posted as hello \\r\\n\\t world
I want to replace these characters but because they are escaped I am not able to use something like gsub(/\s+/, ' ')
Can anyone suggest what would be a ideal way to replace the escaped characters.
Thanks.
If you're getting literally backslash-r you'll need to de-map these:
CONVERT = {
'\r' => "\r",
'\t' => "\t",
'\n' => "\n"
}
CONVERT_RX = Regexp.union(CONVERT.keys)
'this\nis\tinput\r\n'.gsub(CONVERT_RX, CONVERT)
# => "this\nis\tinput\r\n"
You can add more entries to that table as necessary.
From there if you want to strip or convert spaces you can do that as you would normally.

swift: print \r character in debug

I use this text text\r2. And I want to print this in debug and get result:
text\r2
but I get this:
text
2
Try to escape the backslash with another backslash: text\\r2.
The \r will otherwise be interpreted as a line break.
\r in a String literal is a special character and represents a carriage return
See Special Characters in String Literals
String literals can include the following special characters:
* The escaped special characters \0 (null character), \\ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quotation mark) and \' (single quotation mark)
* An arbitrary Unicode scalar, written as \u{n}, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point (Unicode is discussed in Unicode below)
If you want to use in a String Literal a backslash you have to escape it using \\.
So you'll have to write
print("text\\r2")
to get text\r2

Not reading special characters in string literals in swift

I have the following input txt file:
"Hamlet \r William Shakespeare"
"Romeo and Juliet \r William Shakespeare"
"For the whom the bell tolls \r Earnest Hemingway"
I load it into an array and when I output it I get:
Hamlet \r William Shakespeare.
Why is it not reading the carriage return character?
Thanks
If you have \r in a file, it won't be read as the character \r (special return character), it will be read as 2 separate characters \ and r.
You can fix this by replacing the string "\r" with the special charater \r.
content = content.replacingOccurrences(of: "\\r", with: "\r")

Slightly better way of removing new lines

I've got a text/string that contains multiple newlines. Like in the example below :
"This is a test message : \n \n\n \n \n \n \n \n \n \n \n \n  \n \n \n \n \n \n \n
 "
I can gsub all \n with space and remove them all. How can I do the following :
If I see that there is more than two \n, leave only two newlines in the text?
If you want to remove all but the first two newlines you can use the block passed to gsub:
hits = 0
text.gsub(/\n/) { (hits = hits + 1) > 2 ? '' : "\n" }
# => "This is a test message : \n \n "
You can replace any sequences of three or more newlines with nothing in between, by using the following regex (assuming s
contains your string):
s.gsub /\n\n+/, "\n\n"
If you want to allow any amount of interleaving space characters between the newlines and remove that as well, better use:
s.gsub /\n *(\n *)+/, "\n\n"

Resources