title: Python Regex Remove \n
body: <p>I am having an issue. What I am trying to do is sort through data and create new lines at certain points. Currently, my code looks like this:</p>
  
  <pre><code>from __future__ import print_function
  import re
  NDoc = raw_input("Enter name of new document ")+".txt"
  log = open(NDoc, 'w')
  file = raw_input("Enter a file to be sorted ")
  extfile = file+".txt"
  xfile = open(file+".txt")
  
  for line in xfile:
      l=line.strip()
      l=re.sub("\n","",l)
      n=re.sub("(\B)(?=((MTH|HST|ENG)[|]))","\n",line)
  
      if len(n) &gt; 0:
          nl=n.split("\n")
          for item in nl:
                  log.write(item+"\n")
                      #print(item)
  
  print ("The data from",extfile,"has been sorted into",NDoc)
  </code></pre>
  
  <p>Everything is working properly except that after the third term (ENG|) a new line is appearing in my data. For instance, if my datafile was like this:</p>
  
  <pre><code>MTH|lettersandnumbersHST|lettersandnumbersENG|lettersandnumbers
  MTH|lettersandnumbersHST|lettersandnumbersENG|lettersandnumbers
  MTH|lettersandnumbersHST|
  </code></pre>
  
  <p>I would expect it to look like this:</p>
  
  <pre><code>MTH|lettersandnumbers
  HST|lettersandnumbers
  ENG|lettersandnumbers
  MTH|lettersandnumbers
  HST|lettersandnumbers
  ENG|lettersandnumbers
  MTH|lettersandnumbers
  HST|
  </code></pre>
  
  <p>But it is instead giving me this:</p>
  
  <pre><code>MTH|lettersandnumbers
  HST|lettersandnumbers
  ENG|lettersandnumbers
  
  MTH|lettersandnumbers
  HST|lettersandnumbers
  ENG|lettersandnumbers
  
  MTH|lettersandnumbers
  HST|
  </code></pre>
  
  <p>Now I thought doing <code>l=re.sub("\n","",l)</code> would replace all \n  with nothing before my new \n's were added, so why is there still an extra line being made, but only after ENG?</p>
  
  <p>Thank you in advance for any insights.</p>