title: How to complete remove a whole line with multiline regular expressions?
body: <p>I want to remove all lines that include a <code>b</code> in this multiline string:</p>
  
  <pre><code>aba\n
  aaa\n
  aba\n
  aaa\n
  aba[\n\n - optional]
  </code></pre>
  
  <p>Note the file is not <em>necessarily</em> terminated by a newline character, <em>or may have extra line breaks at the end that I want to keep</em>.</p>
  
  <p>This is the expected output:</p>
  
  <pre><code>aaa\n
  aaa[\n\n - as in the input file]
  </code></pre>
  
  <p>This is what I have tried:</p>
  
  <pre><code>import re
  String = "aba\naaa\naba\naaa\naba"
  print(String)
  print(re.sub(".*b.*", "", String))  # this one leaves three empty lines
  print(re.sub(".*b.*\n", "", String))  # this one misses the last line
  print(re.sub("\n.*b.*", "", String))  # this one misses the first line
  print(re.sub(".*b.*\n?", "", String))  # this one leaves an empty last line
  print(re.sub("\n?.*b.*", "", String))  # this one leaves an empty first line
  print(re.sub("\n?.*b.*\n?", "", String))  # this one joins the two remaining lines
  </code></pre>
  
  <p>I have also tried out <code>flags=re.M</code> and various look-aheads and -behinds, but the main question seems to be: how can I remove either the first or the last occurrence of <code>\n</code> in a matching string, depending on which on exists - but not both, if both do exist?</p>