Stephan Olmer
Stephan Olmer

Reputation: 93

How to check unicode string to be letters, spaces and dashes only

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

Answers (2)

psola
psola

Reputation: 23

if you ONLY want to check:

name = u"Василий Соловьев-Седой";
name = name.replace("-","").replace(" ",""); 
name.isalpha()

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

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

Related Questions