From: Jeremy Bopp Date: 2010-12-03T02:34:00+09:00 Subject: Re: Text Parsing Help On 12/2/2010 11:27 AM, Jester Mania wrote: > Greetings, > > I am new to Ruby and programming and am trying to parse a text file, but > encountered some difficulties. > > Basically, the text file contains lines in the following format (where > \n is not really a newline but the text "\n"): > > TextString [SYMBOL]\nDefinition > > I need to replace the text \n with a tab, as I am attempting to separate > all the "tokens" by tabs. The issue here is that \n happens to be a > newline character. I tried searching the forums and tried the following > code: > > lineItem = line.gsub("\\\\n", "\t") > > but it doesn't seem to work and the \n is not being replaced. How can I > convert the \n text into a tab? In Ruby, the literal "\n" is a string consisting of only a newline character. If you want the string to literally be backslash n (\n), then you would use "\\n". The backslash is a special character within string literals, so if you want it to appear literally in your string, you have to escape it with another backslash. Your example in the gsub call above is actually creating a search string of backslash backslash n (\\n) because you have 4 backslashes preceding the n, but that text does not appear in your input. -Jeremy