Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error when substituting a backslash in a string

1 view
Skip to first unread message

Federico Zagarzazú

unread,
Jun 6, 2007, 5:59:33 PM6/6/07
to
Hi,

Why can't I substitute a single backslash in a string?

a = "\\6"
p a # => "\\6"
puts a # => \6
p a.sub('\', '')

Expected: "\6"
Got:
j:4: unterminated string meets end of file
j:4: parse error, unexpected $, expecting ')'

If I change the replacement to 'c', I get a new error:

j:4: parse error, unexpected tIDENTIFIER, expecting ')'
puts a.sub('\', 'c')
^
j:4: unterminated string meets end of file

Both substitution work fine when the pattern is a regular character.
The substitution also works if num('\') is a multiple of 2, example:
a = '\\6'
p a # => "\\6"
puts a # => \6
p a.sub('\\', '')

Expected: "6"
Got: "6"

Thanks for your time.

Harold Hausman

unread,
Jun 6, 2007, 6:10:35 PM6/6/07
to
On 6/6/07, Federico Zagarzazú <fzaga...@gmail.com> wrote:
> Hi,
>
> Why can't I substitute a single backslash in a string?
>
> a = "\\6"
> p a # => "\\6"
> puts a # => \6
> p a.sub('\', '')
>

On that last line, your backslash is escaping the single quote and
causing parsing lossage.

How's about this:
p a.sub("\\", '')

Hope that helps,
-Harold

james.d...@gmail.com

unread,
Jun 6, 2007, 6:34:53 PM6/6/07
to
On Jun 6, 2:59 pm, "Federico Zagarzazú" <fzagarz...@gmail.com> wrote:
> Why can't I substitute a single backslash in a string?
> a = "\\6"
> ...
> p a.sub('\', '')

Because you're essentially escaping a single quote in your String#sub
method call. Double escape that too:

irb(main):001:0> a = "\\6"
=> "\\6"
irb(main):002:0> a.sub('\\', '')
=> "6"

Why is this? Suppose I wanted to make a string with single quotes in
it:

irb(main):003:0> a = '\'hello\''
=> "'hello'"

Federico Zagarzazú

unread,
Jun 6, 2007, 7:27:37 PM6/6/07
to
Thanks for your answers, silly me, got confused :),

But, how does it work?, suppouse you want to convert "fede" to "f\4de"

1) puts 'fede'.sub('e', '\4') # => fde
2) puts 'fede'.sub('e', '\\4') # => fde
3) puts 'fede'.sub('e', '\\\4') # => f\4de
4) puts 'fede'.sub('e', '\\\\4') # => f\4de
5) puts 'fede'.sub('e', '\\\\\4') # => f\de
6) puts 'fede'.sub('e', '\\\\\\4') # => f\de
7) puts 'fede'.sub('e', '\\\\\\\4') # => f\\4de
.
I don't get it..

I was thinking that it might work by making two passes looking for and
converting backslashes, like this:
(7):
first pass : (\\)(\\)(\\)(\4)
\ \ \ \4
second pass: (\\)(\\)4
result : \\4

But I'm not 100% sure, do you know how it works?

Thanks again.

Ryan Mcdonald

unread,
Jun 6, 2007, 8:06:26 PM6/6/07
to
>> p a.sub('\', '')
>>
>
> On that last line, your backslash is escaping the single quote and
> causing parsing lossage.

so.. sometimes escaping happens within single quotes, and sometimes it
doesn't?

a = 'yo\n' # doesn't escape

feels like this should escape:
p a.sub("\\", '')

and this shouldn't:
p a.sub('\\', '')

one more thing to remember I guess.. :-)

--
Posted via http://www.ruby-forum.com/.

james.d...@gmail.com

unread,
Jun 7, 2007, 1:29:40 AM6/7/07
to
On Jun 6, 4:27 pm, "Federico Zagarzazú" <fzagarz...@gmail.com> wrote:
> 1) puts 'fede'.sub('e', '\4') # => fde
> 2) puts 'fede'.sub('e', '\\4') # => fde
> 3) puts 'fede'.sub('e', '\\\4') # => f\4de
> 4) puts 'fede'.sub('e', '\\\\4') # => f\4de
> 5) puts 'fede'.sub('e', '\\\\\4') # => f\de
> 6) puts 'fede'.sub('e', '\\\\\\4') # => f\de
> 7) puts 'fede'.sub('e', '\\\\\\\4') # => f\\4de

You have to be careful with using backslashes with a number for a
replacement string. In a replacement string, \# (where # is a number)
indicates that you want to insert something that was matched in the
original string - the matched things are in parentheses and occur in
regular expressions. For example:

irb(main):001:0> a = "Page 3 of 86"
=> "Page 3 of 86"
irb(main):002:0> a.sub(/^Page (\d+) of (\d+)$/, 'You are on page \1
out of a total of \2 pages')
=> "You are on page 3 out of a total of 86 pages"

The PickAxe has an excellent subject on this and goes over your
question with all sorts of backslash permutations. I guarantee that
it will clarify things further for you:

http://www.rubycentral.com/book/tut_stdtypes.html
(see "Backslash Sequences in the Substitution" section)

Federico Zagarzazú

unread,
Jun 7, 2007, 1:56:53 AM6/7/07
to
Thanks for the link, it cleared my doubts.

Ryan Mcdonald

unread,
Jun 7, 2007, 1:04:47 PM6/7/07
to
From: http://www.rubycentral.com/book/tut_stdtypes.html

"Within single-quoted strings, two consecutive backslashes are replaced
by a single backslash, and a backslash followed by a single quote
becomes a single quote. Double-quoted strings support a boatload more
escape sequences."


easy enough :)

0 new messages