Error when substituting a backslash in a string

  • Thread starter Federico Zagarzazú
  • Start date
F

Federico Zagarzazú

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.
 
H

Harold Hausman

Hi,

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

a =3D "\\6"
p a # =3D> "\\6"
puts a # =3D> \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
 
J

james.d.masters

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'"
 
F

Federico Zagarzazú

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') # =3D> fde
2) puts 'fede'.sub('e', '\\4') # =3D> fde
3) puts 'fede'.sub('e', '\\\4') # =3D> f\4de
4) puts 'fede'.sub('e', '\\\\4') # =3D> f\4de
5) puts 'fede'.sub('e', '\\\\\4') # =3D> f\de
6) puts 'fede'.sub('e', '\\\\\\4') # =3D> f\de
7) puts 'fede'.sub('e', '\\\\\\\4') # =3D> 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.
 
R

Ryan Mcdonald

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.. :)
 
J

james.d.masters

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)
 
F

Federico Zagarzazú

Thanks for the link, it cleared my doubts.

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 =3D "Page 3 of 86"
=3D> "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')
=3D> "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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top