I have the following string:
coolStr = '<!-- \\n @author Phil R\\n @date 6.5.2012\\n @description Me T\\\\nesting S\\\\\\\\ntuff\\n-->'
This string has appears to have been doubly escaped. I’d like to turn the string into:
newCoolStr = "<!-- \n @author Phil R\n @date 6.5.2012\n @description Me T\\nesting S\\\\ntuff\n-->"
So that if you wrote:
puts newCoolStr
You would get:
<!--
@author Phil R
@date 6.5.2012
@description Me T\nesting S\\ntuff
-->
I’ve been failing to do this. The closest I can get is being able to half the backslashes with:
coolStr.gsub(/\\\\/) {'\\'}
But for reasons I don’t understand this does not catch instances of “\\n” and then you end up in this scenario where there is no distinction between line feeds and what originally displayed as “\\\\n”. Example being:
<!-- \\n @author Phil R\\n @date 6.5.2012\\n @description Me T\\nesting S\\\\ntuff\\n-->
Seems like a fairly simple problem but I just can’t seem to get it. Whats the best way of achieving this?
I have come across GreyCat’s solution to:
How do I unescape c-style escape sequences from ruby?
This is exactly what I needed. It removes the double escaping and solves my problem. Upvoted GreyCat’s solution and marking this as the answer to my own question.