in reply to matching comments

This is do the trick for arbitrary comment flags:
/\Q$start\E(.*?)\Q$end\E/sg

I wrote a small test program:
#!/usr/bin/perl #get start and end comment my $start = $ARGV[0]; my $end = $ARGV[1] || "\n"; open(FILE, "test.txt") || die; { local $/ = undef; #set to 'slurp' mode $_ = <FILE>; #read entire file into $_ } close FILE; # #print all comments that are matched in file # while( /\Q$start\E(.*?)\Q$end\E/sg ) { print $&, "\n"; }
For a test.txt I used:
blah blah blah blah blah *** multi line comment **!! blah blah blah blah blah blah blah blah blah blah blah *** inline comment **!! blah blah /* c comment 1 */ blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah /* * c comment 2 */ blah blah // c++ comment 1 blah blah // c++ comment 2
For my execution results I got (my exe is called regex.pl):
prompt$ regex.pl '***' '**!!' *** multi line comment **!! *** inline comment **!! prompt$ regex.pl '/*' '*/' /* c comment 1 */ /* * c comment 2 */ prompt$ regex.pl '//' // c++ comment 1 // c++ comment 2

Replies are listed 'Best First'.
RE: Re: matching comments
by btrott (Parson) on Apr 24, 2000 at 06:54 UTC
    The problem with this--and I don't know if it's actually going to be a problem for the OP, but in general, it might be--is that this will catch comments inside quoted strings. For example:
    char * comptr = "Comment: /* In comment. */";
    Your regular expression will match this, but it isn't actually a comment.

    Again, this may not be an issue for the OP, but if it is, you should take a look at the the faq How do I use a regular expression to strip C style comments from a file?; perhaps you can extend this to your uses.