, label...">

How to check the characters of the word "unicode" in a Python regex?

I have a form with a field:

name = forms.RegexField(regex=r'\w+$', label=u'Name', required=True)

but if I type special characters (for example, for example), then the form does not skip the is_valid () function. How to do it?

+5
source share
2 answers

Activate Unicode matching for \w.

name = forms.RegexField(regex=r'(?u)\w+$', label=u'Name', required=True)
+5
source

Instead of defining a regular expression as a string, you can compile it for the regular expression object by setting the re.U flag :

import re

name_regex = re.compile(r'\w+$', re.U)
name = forms.RegexField(regex=name_regex, label=u'Name', required=True)
+2
source

All Articles