I just test the code above, and it does indeed work.

Out of curiosity I benchmarked the two solutions, and merlyn's is twice as fast! (nice).

However, I would consider mine easier to follow, but maybe that is just because I wrote it. I think I will have to look into using subsrting and pos for performance issues though.
Here is my test code:
#!/usr/bin/perl use Benchmark; #get start and end comment my $start = $ARGV[0]; my $end = $ARGV[1] || "\n"; my $file; open(FILE, "test.txt") || die; { local $/ = undef; #set to 'slurp' mode $file = <FILE>; #read entire file into $_ } close FILE; timethese(100000, { 'parse1' => sub { &parse1($file) }, 'parse2' => sub { &parse2($file) }, }); sub parse1 { my $file = shift; 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"; } return $match; } sub parse2 { my $file = shift; my $inside = 0; my $oldpos = 0; while ($file =~ /(\Q$start\E|\Q$end\E)/g) { if ($1 eq $start) { if (++$inside == 1) { $oldpos = pos($file) - length($start); } } else { if (--$inside == 0) { return substr($file, $oldpos, pos($file)-$oldpos); } } } }
And here is my results (I used the same test.txt as my post above):
prompt% parse.pl '/*' '*/' Benchmark: timing 100000 iterations of parse1, parse2... parse1: 42 wallclock secs (30.07 usr + 0.11 sys = 30.18 CPU) parse2: 17 wallclock secs (13.60 usr + 0.04 sys = 13.64 CPU)

In reply to Re: matching comments by perlmonkey
in thread matching comments by Eugene

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.