Hellnar
Hellnar

Reputation: 64793

Django RegexField

at the registration process I want to enforce typing first and last name of the users. For this purpose, I need to write a regex.

What I need is the username should accept any char input but starting with a number like (1sdasd) or an empty string (like pressing space at this field which makes an empty string " ")

What can be the regular expression for it ?

ps: It should accept non-latin chars such as üÜğĞçÇöÖ (ie r'^\w+$', doesnt work)

Thanks

Upvotes: 2

Views: 1129

Answers (1)

Mark Byers
Mark Byers

Reputation: 838256

You can compile a pattern with the unicode flag:

pattern = re.compile(r"^[^\W\d]\w*$", re.UNICODE)

The first character is matched by [^\W\d], which fails to match things that aren't word characters, and fails to match digits. Everything else is accepted (i.e. word characters that aren't digits). The rest can be any word characters including digits.

Upvotes: 2

Related Questions