From: 7stud -- Date: 2009-05-11T09:13:52+09:00 Subject: Re: gsub pattern substitution and ${...} Sarah Allen wrote: > > Of course, that doesn't work since ${expr} will only eval the expression > within a double quoted string. > > But when it is double quoted, like this: > >>> s.gsub(/href=(['"])([^']*)/, "href=\1#{URI.escape($2)}\3") > => " > > \1 doesn't evaluate to the first match anymore > > I would guess there's some basic string or regex syntax that I'm missing > here. > In double quoted strings escaped characters do not have literal meanings. For instance, in a double quoted string "\n" is not two characters--it is one character that represents a newline. Double quoted strings interpret all escaped characters, which means that \1 gets interpreted into something( but who knows what!). On the other hand, with single quoted strings there are only a couple of automatic substitutions that take place, and interpreting \1 is not one of them. So with single quoted strings \1 means \1. If you need to use double quoted strings, then you need to literally have \1 in your string, which requires the use of additional backslashes to escape the \ in "\1". So try "\\1". With ruby if one backslash is not enough, keep adding more backslashes until whatever you are trying accomplish works! -- Posted via http://www.ruby-forum.com/.