title: Replacing repeated captures
body: <p>This is sort of a follow-up to <a href="https://stackoverflow.com/questions/37375828/python-regex-replace-single-quotes-and-brackets">Python regex - Replace single quotes and brackets</a> thread.</p>
  
  <p><strong>The task:</strong></p>
  
  <p>Sample input strings:</p>
  
  <pre><code>RSQ(name['BAKD DK'], name['A DKJ'])
  SMT(name['BAKD DK'], name['A DKJ'], name['S QRT'])
  </code></pre>
  
  <p>Desired outputs:</p>
  
  <pre><code>XYZ(BAKD DK, A DKJ)
  XYZ(BAKD DK, A DKJ, S QRT)
  </code></pre>
  
  <p>The number of <code>name['something']</code>-like items is <em>variable</em>.</p>
  
  <p><strong>The current solution:</strong></p>
  
  <p>Currently, I'm doing it through <em>two separate <code>re.sub()</code> calls</em>:</p>
  
  <pre><code>&gt;&gt;&gt; import re
  &gt;&gt;&gt;
  &gt;&gt;&gt; s = "RSQ(name['BAKD DK'], name['A DKJ'])"
  &gt;&gt;&gt; s1 = re.sub(r"^(\w+)", "XYZ", s)
  &gt;&gt;&gt; re.sub(r"name\['(.*?)'\]", r"\1", s1)
  'XYZ(BAKD DK, A DKJ)'
  </code></pre>
  
  <p><strong>The question:</strong></p>
  
  <p>Would it be possible to combine these two <code>re.sub()</code> calls into a single one?</p>
  
  <p>In other words, I want to replace something at the beginning of the string and then multiple similar things after, all of that in one go.</p>
  
  <hr>
  
  <p>I've looked into <a href="https://pypi.python.org/pypi/regex" rel="nofollow noreferrer"><code>regex</code> module</a> - it's ability to <a href="https://stackoverflow.com/questions/9764930/capturing-repeating-subpatterns-in-python-regex">capture repeated patterns</a> looks very promising, tried using <code>regex.subf()</code> but failed to make it work.</p>