正規表現の最短一致を使ってこれでいいはず……だと思うんだけど。
def remove_comment(str) str.gsub(/\/\*.*?(\*\/|\z)/,"") end samples = %w( AAA AAA/*BBB*/ AAA/*BBB AAA/*BBB*/CCC AAA/*BBB/*CCC*/DDD*/EEE AAA/a//*BB*B**/CCC ) samples.each do |str| puts str puts " => #{remove_comment(str)}" end
実行。
^o^ >ruby remove_comment.rb AAA => AAA AAA/*BBB*/ => AAA AAA/*BBB => AAA AAA/*BBB*/CCC => AAACCC AAA/*BBB/*CCC*/DDD*/EEE => AAADDD*/EEE AAA/a//*BB*B**/CCC => AAA/a/CCC