From: "Federico Zagarzazú" Date: 2007-06-07T14:56:53+09:00 Subject: Re: Error when substituting a backslash in a string Thanks for the link, it cleared my doubts. On 6/7/07, james.d.masters@gmail.com wrote: > On Jun 6, 4:27 pm, "Federico Zagarzaz�" 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) > > >