title: Replace numeric character references in XML document using Python
body: <p>I am struggling with the following issue: I have an XML string  that contains the following tag and I want to convert this, using cElementTree, to a valid XML document:</p>
  
  <pre><code>&lt;tag&gt;#55296;#57136;#55296;#57149;#55296;#57139;#55296;#57136;#55296;#57151;#55296;
  #57154;#55296;#57136;&lt;/tag&gt;
  </code></pre>
  
  <p>but each # sign is preceded by a &amp; sign and hence the output looks like: &#55296;&#57136;&#55296;&#57149;&#55296;&#57139;&#55296;&#57136;&#55296;&#57151;&#55296;&#57154;&#55296;&#57136;</p>
  
  <p>This is a unicode string and the encoding is UTF-8. I want to discard these numeric character references because they are not legal XML in a valid XML document (see <a href="https://stackoverflow.com/questions/1223391/parser-error-using-perl-xmldom-module-reference-to-invalid-character-number">Parser error using Perl XML::DOM module, &quot;reference to invalid character number&quot;</a>)</p>
  
  <p>I have tried different regular expression to match these numeric character references. For example, I have tried the following (Python) regex:</p>
  
  <pre><code>RE_NUMERIC_CHARACTER = re.compile('&amp;#[\d{1,5}]+;')
  </code></pre>
  
  <p>This does work in regular python session but as soon as I use the same regex in my code then it doesn't work, presumably because those numeric characters have been interpreted (and are shown as boxes or question marks).</p>
  
  <p>I have also tried the unescape function from <a href="http://effbot.org/zone/re-sub.htm" rel="nofollow noreferrer">http://effbot.org/zone/re-sub.htm</a> but that does not work either. </p>
  
  <p>Thus: how can I match, using a regular expression in Python, these numeric character references and create a valid XML document?</p>