"Nikolai Weibull" <ruby-talk / pcppopper.org> schrieb im Newsbeitrag news:20040120150317.GB3012 / puritan.bredbandsbolaget.se... > * Chris Morris <chrismo / clabs.org> [Jan, 19 2004 20:50]: > > 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? > there are better things, yes... > > str.gsub!(/"[^"\\]*(\\.[^"\\]*)*"|\*\//){ |m| m == '\*' ? '*\\' : m } > > will find strings and your pattern fast and efficient while avoiding > prematurely terminated strings (those that contain escaped quotes that > is), > nikolai Did you test that? I'm afraid, it doesn't work: irb(main):001:0> str=' /* comment */ String s = "***/"; ' => " /* comment */ String s = \"***/\"; " irb(main):002:0> str.gsub(/"[^"\\]*(\\.[^"\\]*)*"|\*\//){ |m| m == '\*' ? '*\\' : m } => " /* comment */ String s = \"***/\"; " irb(main):003:0> str == str.gsub(/"[^"\\]*(\\.[^"\\]*)*"|\*\//){ |m| m == '\*' ? '*\\' : m } => true If you want to allow quotes to be escaped, this one is the way to go: irb(main):012:0> puts str.gsub(%r{"([^"\\]|\\")*"|\*/}) {|m| m == '*/' ? '*\\' : m} /* comment *\ String s = "***/"; Regards robert