title: Perl: Regex not grabbing multiline C style comments in code
body: <p>I have a Perl program that:</p>
  <ul>
  <li>Reads a SRC file written in C</li>
  <li>Uses a regex match from SRC file to find specific formatted data to use as the Destination filename</li>
  <li>Opens new Destination file</li>
  <li>Performs another regex match to find all C style comments /* */ that contain a keyword abcd. Note: these comments can be 1 line or more than 1 line so the regex is looking for the first /* and then the keyword abcd and then any amount of text and space before it encounters a closing */</li>
  <li>Writes the regex matches to the destination file</li>
  </ul>
  <pre><code>#!/usr/bin/perl
  use warnings;
  use strict;
  
  my $src = 'D:\\Scripts\\sample.c';
  my $fileName;
  
  # open source file for reading
  open(SRC_FH,'&lt;',$src) or die $!;
  
  while(my $row = &lt;SRC_FH&gt;){
      if ($row =~ /([0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{2}|[0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{3})/){
          $fileName = $1;
      }
  }
  
  my $des = &quot;D:\\Scripts\\&quot; . $fileName . &quot;.txt&quot;;
  
  # open destination file for writing
  open(DES_FH,'&gt;',$des) or die $!;
  
  print(&quot;copying content from $src to $des\n&quot;);
  
  seek SRC_FH, 0, 0;
  
  while(my $row = &lt;SRC_FH&gt;){
      if ($row =~ /(\/\*.*abcd.[\s\S]*?\*\/)/){
          print DES_FH &quot;$1\n&quot;;
      }
  }
  
  # always close the filehandles
  close(SRC_FH);
  
  close(DES_FH);
  print &quot;File content copied successfully!\n&quot;;
  </code></pre>
  <p>My problem is I think because of the way perl code executes although by regex is correct, my destination file is only getting the 1 line comments written to it. Any C style comments that are more than 1 line are not getting written to the destination file. What am I missing in my 2nd if statement?</p>
  <p>I checked my 2nd if statement regex here <a href="https://regexr.com/" rel="nofollow noreferrer">https://regexr.com/</a> and it works as its supposed to capturing multi line C style comments as well as single line comments that also contain the keyword abcd.</p>
  <p>So I tried the 1st suggestion below by zdim. Here is what I used:</p>
  <pre><code>#!/usr/bin/perl
  use warnings;
  use strict;
  
  my $src = 'D:\\Scripts\\sample.c';
  my $fileName;
  my @comments;
  
  # open source file for reading
  open(SRC_FH,'&lt;',$src) or die $!;
  
  while(my $row = &lt;SRC_FH&gt;){
      if ($row =~ /([0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{2}|[0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{3})/){
          $fileName = $1;
      }
  }
  
  my $des = &quot;D:\\Scripts\\&quot; . $fileName . &quot;.txt&quot;;
  
  # open destination file for writing
  open(DES_FH,'&gt;',$des) or die $!;
  
  print(&quot;copying content from $src to $des\n&quot;);
  
  #seek SRC_FH, 0, 0;
  
  my $content = do {
      #read whole file at once
      local $/;
      open (SRC_FH,'&lt;', $src) or die $!;
      &lt;SRC_FH&gt;;
  };
  
  #if($content =~ /(\/\*.*abcd.[\s\S]*?\*\/)/sg){
  #       my @comments = $content;
  #   }
  
  my @comments = $content =~ /(\/\*.*abcd.[\s\S]*?\*\/)/sg;
  
  foreach (@comments){
      print DES_FH &quot;$1\n&quot;;
  }
  
  #while(my $row = &lt;SRC_FH&gt;){
  #   if ($row =~ /(\/\*.*abcd.[\s\S]*?\*\/)/){
  #       print DES_FH &quot;$1\n&quot;;
  #   }
  #}
  
  # always close the filehandles
  close(SRC_FH);
  
  close(DES_FH);
  print &quot;File content copied successfully!\n&quot;;
  </code></pre>
  <p>The result is all the content from sample.c are copied to the destination file. A full 1:1 copy. Where I am looking to pull all comments single line and multiline out of the C file.</p>
  <p>Example 1:
  /* abcd */
  Example 2:
  /* some text
  * some more comments
  abcd and some more comments */</p>
  <p><strong>Final Solution</strong></p>
  <pre><code>#!/usr/bin/perl
  use warnings;
  use strict;
  
  my $src = 'D:\\Scripts\\sample.c';
  my $fileName;
  
  # open source file for reading
  open(SRC_FH,'&lt;',$src) or die $!;
  
  while(my $row = &lt;SRC_FH&gt;){
      if ($row =~ /([0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{2}|[0-9]{2}\.[0-9]{2}\.[0-9]{3}\.[a-z,0-9]{3})/){
          $fileName = $1;
      }
  }
  
  my $des = &quot;D:\\Scripts\\&quot; . $fileName . &quot;.txt&quot;;
  
  # open destination file for writing
  open(DES_FH,'&gt;',$des) or die $!;
  
  print(&quot;copying content from $src to $des\n&quot;);
  
  seek SRC_FH, 0, 0;
  
  my $content = do{local $/; &lt;SRC_FH&gt;};
  
  my @comments = $content =~ /(\/\*.*abcd.[\s\S]*?\*\/)/g;
  
  for(@comments){
      print DES_FH &quot;$_\n&quot;;
  }
  
  # always close the filehandles
  close(SRC_FH);
  
  close(DES_FH);
  print &quot;File content copied successfully!\n&quot;;
  </code></pre>