title: Remove duplicate linebreaks in a string
body: <p>I have some files which could use <code>\r</code>, <code>\n</code>, or <code>\r\n</code> as their line break mode.</p>
  <p>I am trying to change all of them to <code>\r\n</code>, and remove consecutive line breaks. In theory, this is easy, and any number of <a href="http://regex101.com/r/hC2dD7" rel="nofollow noreferrer">very simple regexes</a> should work.</p>
  <p>In practice, though,</p>
  <pre class="lang-py prettyprint-override"><code>text = re.sub(
      reg_exp,
      r'\r\n',
      text)
  </code></pre>
  <p>on this string (showing line-ending characters),</p>
  <pre class="lang-html prettyprint-override"><code>&lt;ul&gt;␍␊
  ␍␊
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍␊
  ␍␊
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍␊
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍␊
  ␍␊
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍␊
  ␍␊
  &lt;/ul&gt;␍␊
  </code></pre>
  <ul>
  <li><p>for <code>reg_exp = r'[\r\n]{2,}'</code>, makes</p>
  <pre class="lang-html prettyprint-override"><code>&lt;ul&gt;␍
  ␍␊
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍
  ␍␊
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍␊
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍
  ␍␊
      &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍
  ␍␊
  &lt;/ul&gt;␍␊
  </code></pre>
  </li>
  <li><p>for <code>reg_exp = r'[\r\n]+'</code>, makes</p>
  <pre class="lang-html prettyprint-override"><code>&lt;ul&gt;␍
  ␍␊
     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍
  ␍␊
     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍
  ␍␊
     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍
  ␍␊
     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;link&lt;/a&gt;&lt;/li&gt;␍
  ␍␊
  &lt;/ul&gt;␍
  ␍␊
  </code></pre>
  </li>
  </ul>
  <p>and I cannot figure out why.</p>
  <p>Is my regex not matching the <code>\r</code> for some reason?</p>