in reply to matching comments

Now I think I should say that I am not aware of any compiler that will compile code with nested comments, so this is probably not a big problem.

However I played around and this seems to do the trick: (just replace the while loop in my code above with this one.)
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"; }
For your tests file I got what you wanted.

For other tests I used this test.txt:
blah blah /* comment 1 */ blah blah /* comment 2 */ blah blah /* outer /* mid /* center */ mid */ outer */
And here are my results:
prompt$ regex.pl '/*' '*/' /* comment 1 */ /* comment 2 */ /* outer /* mid /* center */ mid */ outer */
So enjoy this fanciful result.

I hope this helps.

Replies are listed 'Best First'.
RE: Re: matching comments
by merlyn (Sage) on Apr 25, 2000 at 22:46 UTC
    UNTESTED, but a lot of my stuff works without testing.... :-)
    my $start = "/*"; my $end = "*/"; my $inside = 0; my $oldpos = 0; $_ = "your text /* goes here */ and here"; while (/(\Q$start\E|\Q$end\E)/g) { if ($1 eq $start) { if (++$inside == 1) { $oldpos = pos($_) - length($start); } } else { if (--$inside == 0) { print substr($_, $oldpos, pos()-$oldpos); } } }
Re: Re: matching comments
by Anonymous Monk on Jun 25, 2002 at 03:36 UTC
    That fails on
    /* outer /* mid */ /* mid */ outer */
    Try:
    ($re = $_)=~s/((\Q$start\E)|(\Q$end\E)|.)/${['(','']}[!$2]\Q$1\E${[')' +,'']}[!$3]/gs; $re = join'|',map quotemeta,eval{/$re/}; warn $@ if $@ =~ /unmatched/; print join"\n",/($re)/g,"";