#!/usr/bin/perl use warnings; use strict; while () { my @items = split /'/; until (not @items or $items[0] =~ s/.*?--/--/) { shift @items for 1, 2; # Remove the quoted part, too. } print join "'", @items; } __DATA__ select 'text' from foo --This is a comment select '--Not a valid comment' from foo --But this is select q from z -- as is this select '--This is not a valid comment' from foo select '--Not this' + '--either' from foo select 'qaws' + make from "a" -- comment with 'a' quote select 'a' from 'b' with 'c' -- comment with 'a --' comment