From: Paul Rubel Date: 2004-11-04T03:12:06+09:00 Subject: escaping single quotes in a string with gsub Hi, I'm trying to take a string and escape a single quote if it is not already escaped. My first thought was to look at the string and if I see a quote without a backslash before it put the backslash there. This has a problem when there is an escaped slash before the quote: \\'. I believe the fix should be to look two characters back. If anyone has a canned solution I'm all ears. Would look-behind be an option here out of the box? While I was experimenting I saw some behavior I don't understand and am hoping someone can explain it to me: prubel@cornet /tmp> cat /tmp/t.rb ; ruby /tmp/t.rb 2.times do # replace not a slash followed by a quote with not a slash # and an escaped quote. puts("\\'Summer's Day".gsub(/([^\\\\])'/,"*#{$1}\\\\'*")) puts $1 end #end \'Summe*\'*s Day r \'Summe*r\'*s Day r prubel /tmp> ruby --version ruby 1.8.1 (2004-02-06) [i686-linux-gnu] I'm confused at to why the output is different for the two iterations? Why doesn't the r get placed in the first output? thank you for your help, Paul