title: Best way to deal with "Unescaped braces in regex" inside Perl regex
body: <p>I recently started learning Perl to automate some mindless data tasks. I work on windows machines, but prefer to use Cygwin. Wrote a Perl script that did everything I wanted fine in Cygwin, but when I tried to run it with Strawberry Perl on Windows via CMD I got the "Unescaped left brace in regex is illegal here in regex," error.</p>
  
  <p>After some reading, I am guessing my Cygwin has an earlier version of Perl and modern versions of Perl which Strawberry is using don't allow for this. I am familiar with escaping characters in regex, but I am getting this error when using a capture group from a previous regex match to do a substitution. </p>
  
  <pre><code>open(my $fh, '&lt;:encoding(UTF-8)', $file)
      or die "Could not open file '$file' $!";
  my $fileContents = do { local $/; &lt;$fh&gt; };
  
  my $i = 0;
  while ($fileContents =~ /(.*Part[^\}]*\})/) {
      $defParts[$i] = $1;
      $i = $i + 1;
      $fileContents =~ s/$1//;
  }
  </code></pre>
  
  <p>Basically I am searching through a file for matches that look like:</p>
  
  <pre><code>Part
  {
      Somedata
  }
  </code></pre>
  
  <p>Then storing those matches in an array. Then purging the match from the $fileContents so I avoid repeats. </p>
  
  <p>I am certain there are better and more efficient ways of doing any number of these things, but I am surprised that when using a capture group it's complaining about unescaped characters.</p>
  
  <p>I can imagine storing the capture group, manually escaping the braces, then using that for the substitution, but is there a quicker or more efficient way to avoid this error without rewriting the whole block? (I'd like to avoid special packages if possible so that this script is easily portable.)</p>
  
  <p>All of the answers I found related to this error were with specific cases where it was more straightforward or practical to edit the source with the curly braces.</p>
  
  <p>Thank you!</p>