title: Handling escape characters in a string
body: <p>There are strings from the user input I need to convert. The use case is pretty simple:</p>
  
  <ul>
  <li>When a semicolon is in the string, the string is split into multiple lines.</li>
  <li>When there are two semicolons in a row, they're converted to one.</li>
  </ul>
  
  <p>In theory, no big problem.  I use Python, but I'm sure others with other languages will find this as easy with regular expressions.</p>
  
  <pre><code>import re
  
  def get_lines(text):
      """Return a list of lines (list of str)."""
      command_stacking = ";"
      delimiter = re.escape(command_stacking)
      re_del = re.compile("(?&lt;!{s}){s}(?!{s})".format(s=delimiter), re.UNICODE)
      chunks = re_del.split(text)
  
      # Clean the double delimiters
      for i, chunk in enumerate(chunks):
          chunks[i] = chunk.replace(2 * command_stacking, command_stacking)
  
      return chunks
  </code></pre>
  
  <p>That seems to work:</p>
  
  <pre><code>&gt;&gt;&gt; get_lines("first line;second line;third line with;;a semicolon")
  ['first line', 'second line', 'third line with;a semicolon']
  &gt;&gt;&gt;
  </code></pre>
  
  <p>But when there's three or four semicolons, it doesn't behave as expected.</p>
  
  <p>The multiple semicolons are ignored by the regular expression (as they should), but when replacing <code>;;</code> by <code>;</code>, <code>;;;</code> is replaced by <code>;;</code>, <code>;;;;</code> is replaced by <code>;;...</code> and so on.  It would be great if 2 was replaced by 1, 3 by 2, 4 by 3... that's something I could explain to my users.</p>
  
  <p>What would be the best solution to do that?</p>
  
  <p>Thanks for your help,</p>