Lessons · Regex · \w is not only English
A letter is not always a-z
In Python 3, \w means a letter in any script, plus digits and the underscore. [a-z] means twenty-six ASCII characters and nothing else.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Names. A validator built from [a-zA-Z]+ turns away real customers, and it does it quietly on the ones whose names you did not think of.
How to think about it
Ask whether you mean 'a letter' or 'one of these characters'. Write the one you mean; they agree on English and part company everywhere else.
Worked example
re.findall(r'\w+', 'caf\xe9 ni\xf1o')Both words, whole.
re.findall(r'[a-z]+', 'caf\xe9 ni\xf1o')The range cut each name where the accent was.
bool(re.fullmatch(r'[a-zA-Z]+', 'ni\xf1o'))Which is a validator refusing a real name.
Your turn
Match runs of letters in any language.
+
Test a pattern against real text
The trap
re.I does not help: it pairs a-z with A-Z and adds nothing new. The range is the problem, not the case.
Practise \w is not only English on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.