I have found this excellent guide: http://www.regular-expressions.info/unicode.html#category that gives some hints on how to match non letters with the following regex:
\P{L}
But this regex will consider non letters also à encoded as U+0061 U+0300 (if I understood well).
For example using regex module in python the following snippet:
all_letter_doc = regex.sub(r'\P{L}', ' ', doc)
will transform purè in pur
In the guide is provided how to match all letters with the following:
\p{L}\p{M}*+
and in practice I need the negation of that but I do not know how to obtain it.
Since you are using Python 2.x, your r'\P{L}' is a byte string, while the input you have is Unicode. You need to make your pattern a Unicode string. See the PyPi regex reference:
If neither the
ASCII,LOCALEnorUNICODEflag is specified, it will default toUNICODEif the regex pattern is a Unicode string andASCIIif it’s a bytestring.
Thus, you need to use ur'\P{L}' and a u' ' replacement pattern.
In case you want to match 1+ chars other than letters and diacritics, you will need ur'[^\p{L}\p{M}]+' regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With