while( $file =~ /\Q$start\E(.*?)\Q$end\E/sg ) { $a = $1; $match = $&; #look for more start tags in what we matched while( $a =~ /\Q$start\E/sg ) { #balance the ending comments $file =~ /.*?\Q$end\E/sg; $match .= $&; } print $match, "\n"; } #### blah blah /* comment 1 */ blah blah /* comment 2 */ blah blah /* outer /* mid /* center */ mid */ outer */ #### prompt$ regex.pl '/*' '*/' /* comment 1 */ /* comment 2 */ /* outer /* mid /* center */ mid */ outer */