varad
varad

Reputation: 8029

django regular expression only to match alphanumeric not underscore

I have a model field where I want user only to enter alphanumeric character. I have set regex like:

validators.RegexValidator(r'^[\w]+$'

It also takes _ but I only want to take aplha numeric characters not _. What might be the regex for it ?

Upvotes: 1

Views: 2298

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627128

To disallow _ with \w, you may use the opposite class \W (non-word shorthand character class) in a negated character class and exclude a _ by adding it to the negated character class:

r'^[^\W_]+$'
    ^^^^

This pattern matches start of string (^), one or more characters other than non-word chars (=matches all word chars) and other than _ (so, all that are \w but excluding a _), and then the end of string.

Or (if you only work with ASCII) just use

r'(?i)^[a-z0-9]+$'

As \w = [a-zA-Z0-9_] without the re.UNICODE flag.

The (?i)^[a-z0-9]+$ matches both lower- and uppercase ASCII letters due to the case insensitive inline modifier (?i).

Upvotes: 2

Gocht
Gocht

Reputation: 10256

You could use this regex:

[A-Za-z0-9]+

If you don't want uppercase avoid A-Z

Upvotes: 0

alecxe
alecxe

Reputation: 474071

We usually use a simple [A-Za-z0-9] regex which is essentially \w but without the underscore (assuming you are not setting any specific locales or a unicode flag):

\w

When the LOCALE and UNICODE flags are not specified, matches any alphanumeric character and the underscore; this is equivalent to the set [a-zA-Z0-9_]. With LOCALE, it will match the set [0-9_] plus whatever characters are defined as alphanumeric for the current locale. If UNICODE is set, this will match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties database.

Also see Regular Expressions: How to Express \w Without Underscore for other options.

Upvotes: 2

Related Questions