title: How to print only the capture groups of all matches in perl?
body: <p>How to print only the capture groups of all matches in perl? <code>/g</code> doesn't seem to work.</p>
  <p><em>I don't think I'm doing any of it correctly, using <code>if</code> statements, this is why am asking. How is the proper way to do it?</em> (I can't find anything on the Internet that helps and I am struggling for hours to make it finally work.)</p>
  <pre class="lang-perl prettyprint-override"><code>$LONG_REGEX_WITH_TWO_CAPTURING_GROUPS=&quot;&quot;;
  $file1=&quot;file1.html&quot;;
  
  /* This part is complicated, this is why I said nothing 
   * about the two, but here is the result:
   *
   * Basically $2 (a letter) + whitespace + $1 (a filename)
   * a file.txt
   * b anotherfile.txt
   * c 3rdfile.txt
   * d 4thfile.txt
   * 
   * I want it to become&gt;
   * a - (A specific part of the text in file.txt)
   * b - (A specific part of the text in anotherfile.txt)
   * etc.
  */
  
  my $content1 = do { open my $fh, '&lt;', $file1 or die $!; local $/; &lt;$fh&gt;; };
  
  if ( $content1 =~ /$LONG_REGEX_WITH_TWO_CAPTURING_GROUPS/g ) {
      # Print the letter first ($2).
      print &quot;$2 - &quot;;
      # Open the corresponding file (it's name is $1).
      my $content2 = do { open my $fh, '&lt;', $1 or die $!; local $/; &lt;$fh&gt;; };
      # Try to complete the task.
      if ( $content2 =~ /$SECOND_REGEX/g ) {
          print &quot;$1\n&quot;; # There is just one capturing group.
      }
  }
  </code></pre>
  <p>However, this only prints the first match, even if it has a global flag.</p>
  <p>As in:</p>
  <p><code>a - The desired text.</code></p>
  <p>Nevermind the code, the question is very simple: How to print only the content from the capture groups, but from all of the matches (or making it so it matches everything in the file)?</p>
  <p>Thank you!</p>
  <p><strong>I'm editing so I can put the code here:</strong></p>
  <pre class="lang-perl prettyprint-override"><code>#!/usr/bin/perl
  
  $file1=&quot;file1.html&quot;;
  my $content1 = do { open my $fh, '&lt;', $file1 or die $!; local $/; &lt;$fh&gt;; };
  
  foreach ( $content1 =~ m/LONG_REGEX_WITH_TWO_CAPTURING_GROUPS/g ) {
      # If I were to put a print &quot;$content1&quot;; here, the program would have
      # no output. Here is the problem, the question still remains.
      print &quot;$2 - &quot;;
      my $content2 = do { open my $fh, '&lt;', $1 or die $!; local $/; &lt;$fh&gt;; };
      foreach ( $content2 =~ m/SECOND_REGEX&gt;/g ) {
          print &quot;$1\n&quot;; # There is just one capturing group.
      }
  }
  </code></pre>
  <p><strong>This worked for me:</strong></p>
  <pre class="lang-perl prettyprint-override"><code>#!/usr/bin/perl
  
  $file1=&quot;file1.html&quot;;
  my $content1 = do { open my $fh, '&lt;', $file1 or die $!; local $/; &lt;$fh&gt;; };
  while ( $content1 =~ /LONG_REGEX_WITH_TWO_CAPTURING_GROUPS/g ) 
      print &quot;$2 - &quot;;
      my $content2 = do { open my $fh, '&lt;', &quot;../../VT/$1&quot; or die $!; local $/; &lt;$fh&gt;; };
      while ( $content2 =~ /SECOND_REGEX/g ) {
          print &quot;$1\n\n&lt;br/&gt;&quot;; # There is just one capturing group.
      }
  }
  </code></pre>