From: Rob Biedenharn Date: 2009-01-20T09:51:13+09:00 Subject: Re: Gsub!("\n","\n") On Jan 19, 2009, at 7:19 PM, Simon Tan wrote: > Hi all, I'm not sure why but I assumed gsub! would allow me to replace > \n with line feeds or \r for carriage returns. > > How do I gsub! and replace everything with a nonvisible character like > newline? I was using this results.gsub!("\n","\n") but obviously that > does not work :( also tried results.gsub!("\n",10.chr). No go. > > Kinda of frustrating for something so simple. I guess I could > always do > some recursion. Thanks. > -- Are you trying to turn this: Hello, World Into something like: Hello,\nWorld If so, you need to change "\n" (1 character string) into "\\\\n" (3 character string, two escaped backslashs and the letter 'n'). The replacement string will undergo two different interpretations: as a string literal "\\\\n" becomes \\n which when interpreted as a replacement for a regexp undergoes another round (so that things like \1 and \2 can reference groups in the regexp) and becomes \n Yes, this is tricky, but it's because there are two levels of escaping going on and both use the backslash. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com