From: Simon Strandgaard Date: 2004-01-20T06:40:09+09:00 Subject: Re: regex help On Tue, 20 Jan 2004 04:38:12 +0900, Chris Morris wrote: > I need a re such that: > > ' /* comment */ String s = "***/"; '.gsub(re, "*\\") > > returns: > > ' /* comment *\ String s = "***/"; ' > > I want to find all *\ instances that are not enclosed in double-quotes. > Or is this one of those problems ill-suited for a regex? possible.. but difficult. I spend this evening making some _broken_ experiments. -- Simon Strandgaard server> ruby test_main.rb Loaded suite TestMain Started test_balance_bad1(TestMain): . test_balance_bad2(TestMain): F test_balance_bad3(TestMain): F test_balance_ok1(TestMain): F test_balance_ok2(TestMain): . Finished in 0.030393 seconds. 1) Failure: test_balance_bad2(TestMain) [test_main.rb:25:in `assert_x' test_main.rb:33:in `test_balance_bad2']: <["xx ", "*/"]> expected but was . 2) Failure: test_balance_bad3(TestMain) [test_main.rb:25:in `assert_x' test_main.rb:37:in `test_balance_bad3']: <["xx /* /* */ ", "*/"]> expected but was <["/* /* "]>. 3) Failure: test_balance_ok1(TestMain) [test_main.rb:25:in `assert_x' test_main.rb:41:in `test_balance_ok1']: expected but was <["/* "]>. 5 tests, 5 assertions, 3 failures, 0 errors server> expand -t2 test_main.rb require 'test/unit' class TestMain < Test::Unit::TestCase def mk_re comment_begin = '\/\*' # /* comment_end = '\*\/' # */ re = / ( #{comment_begin} .*? ) #{comment_end} .*? (?! #{comment_begin} ) (?= #{comment_end} ) /x re end def assert_x(expected, input) actual = mk_re.match(input) if actual actual = actual.to_a actual.shift end assert_equal(expected, actual) end def test_balance_bad1 s = 'xx /* comment */ String s = "***/"; ' assert_x(['/* comment '], s) end def test_balance_bad2 s = 'xx */ */ String s = "***/"; ' assert_x(['xx ', '*/'], s) end def test_balance_bad3 s = 'xx /* /* */ */' assert_x(['xx /* /* */ ', '*/'], s) end def test_balance_ok1 s = ' /* */ /* */ ' assert_x(nil, s) end def test_balance_ok2 s = 'xx /* /* */ ' assert_x(nil, s) end end if $0 == __FILE__ require 'test/unit/ui/console/testrunner' Test::Unit::UI::Console::TestRunner.run(TestMain, 3) end server>