#!/usr/bin/perl use warnings; use strict; while () { my $last = (split /'/)[-1]; print $1 if $last =~ /(--.*)/; } __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