In .net you can use \p{L}
to match any letter, how can I do the same in Python? Namely, I want to match any uppercase, lowercase, and accented letters.
Unicode Regular Expressions. Unicode is a character set that aims to define all characters and glyphs from all human languages, living and dead. With more and more software being required to support multiple languages, or even just any language, Unicode has been strongly gaining popularity in recent years.
Unicode is an international character encoding standard that provides a unique number for every character across languages and scripts, making almost all characters accessible across platforms, programs, and devices.
\p{L} matches a single code point in the category "letter". \p{N} matches any kind of numeric character in any script.
Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .
Python's re
module doesn't support Unicode properties yet. But you can compile your regex using the re.UNICODE
flag, and then the character class shorthand \w
will match Unicode letters, too.
Since \w
will also match digits, you need to then subtract those from your character class, along with the underscore:
[^\W\d_]
will match any Unicode letter.
>>> import re
>>> r = re.compile(r'[^\W\d_]', re.U)
>>> r.match('x')
<_sre.SRE_Match object at 0x0000000001DBCF38>
>>> r.match(u'é')
<_sre.SRE_Match object at 0x0000000002253030>
PyPi regex module supports \p{L}
Unicode property class, and many more, see "Unicode codepoint properties, including scripts and blocks" section in the documentation and full list at http://www.unicode.org/Public/UNIDATA/PropList.txt. Using regex
module is convenient because you get consistent results across any Python version (mind that the Unicode standard is constantly evolving and the number of supported letters grows).
Install the library using pip install regex
(or pip3 install regex
) and use
\p{L} # To match any Unicode letter
\p{Lu} # To match any uppercase Unicode letter
\p{Ll} # To match any lowercase Unicode letter
\p{L}\p{M}* # To match any Unicode letter and any amount of diacritics after it
See some usage examples below:
import regex
text = r'Abc-++-Абв. It’s “Łąć”!'
# Removing letters:
print( regex.sub(r'\p{L}+', '', text) ) # => -++-. ’ “”!
# Extracting letter chunks:
print( regex.findall(r'\p{L}+', text) ) # => ['Abc', 'Абв', 'It', 's', 'Łąć']
# Removing all but letters:
print( regex.sub(r'\P{L}+', '', text) ) # => AbcАбвItsŁąć
# Removing all letters but ASCII letters:
print( regex.sub(r'[^\P{L}a-zA-Z]+', '', text) ) # => Abc-++-. It’s “”!
See a Python demo online
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