title: Perl: How to extract the range of lines starting from "IF" to "END-IF" ignoring the lines starting with a *
body: <p>My code tries to extract the range even if the line starts with a * Here is my code:</p>
  
  <pre><code>while (&lt;FILE1&gt;) {
  
      $_ =~ s/^\s+//; #remove leading spaces
      $_ =~ s/\s+$//; #remove trailing spaces
  
      if (/IF/ .. /END-IF/) {
  
          if($_ =~ m/END-IF/) {
  
              $flag = 1;
          }
          print FINAL "$_\n";
  
          if ($flag == 1) {
  
              $flag = 0;
              print FINAL "\n\n";
          }
      }
  }
  close FINAL;
  close FILE1;
  </code></pre>
  
  <p>My FINAL output file should contain only the the ranges between all IF's and END-IF's seperated by \n\n  AND if there is an IF within an IF block, The range starting from the first if to the line before the second IF should be saved in FINAL seperated by \n\n</p>