#Content of the file test.txt Blah blah blah //Hello blah blah Blah // single line c++ comment more blahs "quoted string with /* " blah blah blah blah /* single line c comment */ blah blah blah /* multi line c style comment */ some more blahs // another single line c++ comment blah blah blah #### use strict; undef $/;#In order to read the whole file at once open(F,"test.txt"); my @matches = =~ m{ ( /\* .*? \*/) | ( \/\/[^\n]*) | " (?: [^"\\]* | \\. )* " | ' (?: [^'\\]* | \\. )* ' | . [^/"']* }xgs; @matches = grep {defined $_} @matches; #get rid of undefs my $linenum = 1; foreach my $match (@matches) { $slurpedfile =~ /\Q$match/; my $before = $`; $slurpedfile = $'; my $matched = $&; $linenum += $before =~ tr/\n/\n/; print "Line $linenum\t$match\n"; $linenum += $match =~ tr/\n/\n/; } #### Line 12 //Hello Line 23 // single line c++ comment Line 34 /* single line c comment */ Line 45 /* Line 46 multi line Line 47 c style comment */ Line 58 // another single line c++ comment #### Line 12 //Hello Line 23 // single line c++ comment Line 34 /* single line c comment */ Line 45 /* multi line c style comment */ Line 58 // another single line c++ comment