#! perl use strict; use warnings; my @selects = ("select 'text' from foo --This is a comment", "select '--Not a valid comment' from foo --But this is", "select '--This is not a valid comment' from foo", "select '--Not this' + '--either' from foo"); for my $select (@selects) { my ($comment) = $select =~ / ( -- [^']+ ) $ /x; print $comment, "\n" if $comment; }