From: "Jesús Gabriel y Galán" Date: 2013-01-17T18:27:45+09:00 Subject: Re: Escaped backslashes in input strings - newbie question On Thu, Jan 17, 2013 at 10:05 AM, John Sampson wrote: > On 16/01/2013 21:53, Robert Klemme wrote: >> >> On Wed, Jan 16, 2013 at 6:55 PM, John Sampson >> wrote: >>> >>> Input would be, for example, as (part of) a line in a text file, >>> "\u99m\UTc". The desired output in this case would be "99mTc". >>> >>> The objective is to delete the "\u" and the "\U" and any other single >>> letters where they are preceded by a backslash. >> >> OK, understood so far. This is what you wrote right from the start. >> But what's actually not working? >> >> robert >> > If, at IRB, I write: x = "\b99m\BTc" (not \u which gives an 'invalid Unicode > escape') > and then: a.gsub(/\\./,"") > the answer is "\b99mBTc". The problem is that what you wrote is not what you meant. In Ruby code: "\b" is a single character: 1.9.2p290 :002 > "\b".length => 1 To have the same thing you will have when reading from a file, you should escape the \ in string literals: 1.9.2p290 :003 > "\\b".length => 2 So, 1.9.2p290 :005 > "\\b1234\\c5678".gsub(/\\./, "") => "12345678" > I am trying to get "99mTc", which I do get provided that I escape "\b" and > "\B" with > extra backslashes, but in file input these extra backslashes will be absent. > I think on > investigation that the extra backslashes will not be needed in that, but not > necessarily > any other, context. Exactly, source code is different from a file that you read. Jesus.