Reputation: 93
I've read a great solution for unicode strings here, but I need to check entire string to be letters or spaces or dashes and I can't think of any solution. The example is not working as I want.
name = u"Василий Соловьев-Седой"
r = re.compile(r'^([\s\-^\W\d_]+)$', re.U)
r.match(name) -> None
Upvotes: 1
Views: 1075
Reputation: 23
if you ONLY want to check:
name = u"Василий Соловьев-Седой";
name = name.replace("-","").replace(" ","");
name.isalpha()
Upvotes: 0
Reputation: 336378
r = re.compile(r'^(?:[^\W\d_]|[\s-])+$', re.U)
[^\W\d_]
matches any letter (by matching any alphanumeric character except for digits and underscore).
[\s-]
of course matches whitespace and dashes.
Upvotes: 4