title: How do I avoid removing blocks of text between Regex flags in my text file using the the Python re library?
body: <p>I am currently trying to use the Python <code>re</code> library to cut out blocks of text files based on unique flags I put inside of those files. However, if I include multiple instances of flagged blocks in my file, I find that all flagged blocks get lumped together as one big block and deleted.</p>
  <p>Consider this example text file, call it <code>test.txt</code>:</p>
  <pre><code>Do, a deer, a female deer
  ### mark
  Re, a drop of golden sun
  ### end mark
  Mi, a name I call myself
  Fa, a long long way to run
  ### mark
  So, a needle pulling thread
  La, a note to follow So
  Ti, a drink with jam and bread
  ### end mark
  Now that brings us back to Do
  </code></pre>
  <p>I'm using the following code to cut out the blocks of text I want:</p>
  <pre><code>def replace_file_text(path, regex, repl, flags=0):
      old_text = &quot;&quot;
      with open(path, &quot;r&quot;, encoding=&quot;utf-8&quot;) as f:
          old_text = f.read()
      new_text = re.sub(regex, repl, old_text, flags=flags)
      with open(path, &quot;w&quot;, encoding=&quot;utf-8&quot;) as f:
          f.write(new_text)
  
  replace_file_text(&quot;test.txt&quot;, r&quot;### mark.*### end mark&quot;, &quot;&quot;, re.DOTALL)
  </code></pre>
  <p>What I want the text file to be converted to is:</p>
  <pre><code>Do, a deer, a female deer
  Mi, a name I call myself
  Fa, a long long way to run
  Now that brings us back to Do
  </code></pre>
  <p>But what I actually end up getting is:</p>
  <pre><code>Do, a deer, a female deer
  Now that brings us back to Do
  </code></pre>
  <p>I see what the problem is, but I'm not sure how to avoid counting instances of my flags as text to delete.</p>
  <p>Originally, I had coded a shell script to cut out these blocks of text using the <code>sed</code> text editor, and it had worked pretty well:</p>
  <pre><code>sed &quot;/### mark/,/### end mark/d&quot; test.txt &gt; new-test.txt
  </code></pre>
  <p>Can someone explain to me why the latter approach works while the former doesn't? And does anyone have any suggestions for a relatively clean way to avoid deleting the text between my flags in the Python version?</p>