use warnings; use strict; while () { s/( (?:(?:[^\/][^\/]*?|)*? # anything, except comment symbol ("|').*?\2 # quoted string (and take everything) (?:[^\/][^\/]*?|)*? # anything, except comment symbol )*? ) (?: \/\/ # comment symbol [^\\]* # anything, except continuation )??$ /$1/x; print; } __DATA__ /* // */ /* */ *str = "//\"//";