title: python regex match and replace
body: <p>I need to find, process and remove (one by one) any substrings that match a rather long regex:</p>
  
  <pre><code># p is a compiled regex
  # s is a string  
  while 1:
      m = p.match(s)
      if m is None:
          break
      process(m.group(0)) #do something with the matched pattern
      s = re.sub(m.group(0), '', s) #remove it from string s
  </code></pre>
  
  <p>The code above is not good for 2 reasons:</p>
  
  <ol>
  <li><p>It doesn't work if m.group(0) happens to contain any regex-special characters (like *, +, etc.).</p></li>
  <li><p>It feels like I'm duplicating the work: first I search the string for the regular expression, and then I have to kinda go look for it again to remove it.</p></li>
  </ol>
  
  <p>What's a good way to do this?</p>