#!/usr/bin/perl use strict; my $bob = "/* THis ** comment** is\n\nMy\ngreatest\n Comment** */"; my $comment = 0; my $line_num = 1; while($bob) { my @match_list; print "STARTLOOP matching $bob\n"; if ($bob =~ /^\/\*/) { push @match_list,{rest=>$',code=>"\$comment=1",matched=>$&}; print "start comment matched: $&\n"; } if($comment && $bob =~ /^[^*\n]*/) { push @match_list,{rest=>$',code=>"",matched=>$&}; print "not star matched: $&\n"; } if($comment && $bob =~ /^\*[^*\n]*/) { push @match_list,{rest=>$',code=>"",matched=>$&}; print "star with no slash matched: $&\n"; } if($comment && $bob =~ /^\n/) { push @match_list,{rest=>$',code=>"++\$line_num;",matched=>$&}; print "newline matched: $&\n"; } if($comment && $bob =~ /^\*\//) { push @match_list,{rest=>$',code=>"\$comment=0",matched=>$&}; print "end comment matched: $&\n"; } my $max_length = 0; my $match; #should be priority queue.. but I'm too tired to # use that module now... foreach $match (@match_list) { if(length($match->{matched}) > $max_length) { $max_length = length($match->{matched}); } } foreach $match (@match_list) { if(length($match->{matched}) == $max_length) { eval $match->{code}; $bob = $match->{rest}; } } } print "line num is $line_num\n";