1

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 ?

varad
  • 7,309
  • 20
  • 60
  • 112

3 Answers3

2

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.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2

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).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

You could use this regex:

[A-Za-z0-9]+

If you don't want uppercase avoid A-Z

Gocht
  • 9,924
  • 3
  • 42
  • 81