From: Paul Lutus Date: 2006-09-16T07:45:55+09:00 Subject: Re: No regex backreference with four backslashes gabriel.birke@gmail.com wrote: > Consider the following test case: > > require 'test/unit' > class RegexTest < Test::Unit::TestCase > def test_escaping > numbers = "12345" > assert_equal "12345", numbers.gsub(/(2|4)/, '\1') > assert_equal "12345", numbers.gsub(/(2|4)/, "\\1") > assert_equal "1\\ 23\\ 45", numbers.gsub(/(2|4)/, '\\ \1') > assert_equal "1\\ 23\\ 45", numbers.gsub(/(2|4)/, "\\ \\1") > assert_equal "1\\23\\45", numbers.gsub(/(2|4)/, '\\\1') > assert_equal "1\\23\\45", numbers.gsub(/(2|4)/, "\\\\1") > end > end > require 'test/unit/ui/console/testrunner' > Test::Unit::UI::Console::TestRunner.run(RegexTest) > > The last two assertions fail (With the message <"1\\23\\45"> expected > but was <"1\\13\\15">.) - but why? > > Is this a bug in the regex implementation or is there something wrong > with my regular expression or substitution string? To find out how your strings are being parsed, print them out. Then print out the result of the regexes directly, rather than relying on an assertion. numbers.gsub(/(2|4)/,"\\\1") "1\\\0013\\\0015" numbers.gsub(/(2|4)/,"\\\\1") "1\\13\\15" The best "test suite" is your eyes. -- Paul Lutus http://www.arachnoid.com