I would do the regexp like this: s~\s*//.*~~; Using a delimiter other than / keeps you from having to escape the slashes, otherwise it'd be like s/\s*\/\/.*//, which is kinda confusing. But yah the rest is just that simple. Except you aren't considering newlines: