From: Eric Schwartz Date: 2003-06-11T02:26:57+09:00 Subject: Re: OT: Regexp question Dominik Werder writes: > And please show me how I can easily substitute a single quotation mark by a > backslash and single qotation mark to write it safely into a database..... > Finally I got this one, but it's damn ugly: .gsub("'") { "\\'" } > Because \' is a special character combination and I don't know how to > escape it... \\' doesn't work Depending on your database, a regex may or may not work. Instead, use your databases' DBH module to do all the work: $ irb irb(main):001:0> require 'dbi' => true irb(main):002:0> dsn = "DBI:mysql:database=test;host=test" => "DBI:mysql:database=test;host=test" irb(main):003:0> dbh = DBI.connect(dsn,'test','test') => #, @trace_mode=2, @handle=#, @mutex=#>> irb(main):004:0> str = "testing 'this' quote style" => "testing 'this' quote style" irb(main):005:0> dbh.quote(str) => "'testing \\'this\\' quote style'" No muss, no fuss, and it'll catch things you might not have thought needed escaping. BTW, it's easier for other people who are looking for the same problem to find the answers if you use slightly more descriptive Subject: lines than "regexp question". Imagine if you're looking in the archives for a similar problem, and you see "OT: Regexp question". Don't you suppose you would find Subject: lines like "removing numbers with regexp" and "escaping quotes for database" more useful? Not a flame, just a thought. -=Eric -- Come to think of it, there are already a million monkeys on a million typewriters, and Usenet is NOTHING like Shakespeare. -- Blair Houghton.