Python regex to remove punctuation except from URLs and decimal numbers
<p>People,</p>

<p>I need a regex to remove punctuation from a string, but keep the accents and URLs. I also have to keep the mentions and hashtags from that string.</p>

<p>I tried with the code above but unfortunately, it replaces the characters with accents but I want to keep the accents.</p>

<pre><code>import unicodedata

if __name__ == "__main__":
    text = "Apenas um teste com acentuaÃ§Ã£o. Para pontuaÃ§Ã£o tambÃ©m! #python @stackoverflow http://xyhdhz.com.br" 
    text = unicodedata.normalize('NFKD', text).encode('ascii','ignore')
    print text
</code></pre>

<p>The output for the following text <strong>"Apenas um teste com acentuaÃ§Ã£o. Para pontuaÃ§Ã£o tambÃ©m! #python @stackoverflow <a href="http://xyhdhz.com.br" rel="nofollow">http://xyhdhz.com.br</a>"</strong> should be <strong>"Apenas um teste com acentuaÃ§Ã£o Para pontuaÃ§Ã£o tambÃ©m #python @stackoverflow <a href="http://xyhdhz.com.br" rel="nofollow">http://xyhdhz.com.br</a>"</strong></p>

<p>How could I do that?</p>

<p>You can use Python's <a href="https://docs.python.org/3/library/re.html#module-contents" rel="nofollow">regex module</a> and <code>re.sub()</code> to replace any characters you want to get rid of. You can either use a blacklist and replace all the characters you don't want, or use a whitelist of all the characters you want to allow and only keep those. </p>

<p>This will remove anything in the bracketed class of characters:</p>

<pre><code>import re

test = r'#test.43&amp;^%Ã , Ã¨, Ã¬, Ã², Ã¹, Ã, Ã, Ã, Ã, ÃÃz'
out = re.sub(r'[/.!$%^&amp;*()]', '', test)
print(out)
# Out: #test43Ã  Ã¨ Ã¬ Ã² Ã¹ Ã Ã Ã Ã ÃÃz
</code></pre>

<p>(tested with Python 3.5)</p>

<p>To keep URLs you will have to do a little more processing to check for that format (which is pretty varied). What kind of input/output are you looking for in that case?</p>

<p>edit: based on your added input example:</p>

<pre><code>test = "Apenas um teste com acentuaÃ§Ã£o. Para pontuaÃ§Ã£o tambÃ©m! #python @stackoverflow" 
# Out: Apenas um teste com acentuaÃ§Ã£o Para pontuaÃ§Ã£o tambÃ©m #python @stackoverflow
</code></pre>

python regex nltk