Brian Candler
5/3/2007 7:03:00 PM
On Fri, May 04, 2007 at 03:16:37AM +0900, Suraj Kurapati wrote:
> Phil Swenson wrote:
> > I need to search a file for text "InstallDir=" + newline character and
> > replace it with something like: InstallDir=C\:\\mws71 + newline
>
> >> "InstallDir=\n".gsub /(InstallDir=)(\n)/, '\1C\:\mws71\2'
> => "InstallDir=C\\:\\mws71\n"
>
> > My own experimentation with this had problems with writing the back-
> > slashes to the file (it's a requirement of the format to have the back-
> > slashes as in my example). The string had the back-slashes in it but
> > they didn't get persisted to the file correctly.
>
> In a single-quoted string, one backslash outputs one backslash.
Except where followed by a single quote or another backslash.
irb(main):001:0> '\\'.length
=> 1
irb(main):002:0> '\''.length
=> 1
irb(main):003:0> '\n'.length
=> 2