Regular Expression with Variables.

20 views
Skip to first unread message

Arun S

unread,
May 19, 2016, 1:37:51 AM5/19/16
to Django users
Hi,

I have a Regular Expression something like this:
regex=r'^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!$%^&*?@#-_+=])'
               r'.*$',

In this, i would like to read the values from the Django Setting file on the Max and Min Length and provide them as my input instead of Hard Coding.

How can a Variable be provided/escaped in a regex.
Can some one throw some light on this ?

Cheers
Arun


Stephen J. Butler

unread,
May 19, 2016, 1:56:58 AM5/19/16
to django...@googlegroups.com
I think I'd just use format() on the regex, being careful to escape '{' and '}':

regex_f=r'^.*(?=.{{{MIN},{MAX}}})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!$%^&*?@#-_+=])'
regex=regex_f.format(MIN=settings.MY_RE_MIN, MAX=settings.MY_RE_MAX)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/90556052-063e-4307-b095-c37a9920399b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arun S

unread,
May 19, 2016, 2:53:03 AM5/19/16
to Django users
I Did try this, i think i am not doing it right:
Could you tell what is that i am doing is wrong.
I am now not able to provide multiple Regex in the forms.RegexField using the above method.



     regex_f=r'^.*(?=.{{{MIN},{MAX}}})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!$%^&*?@#-_+=])'
     old_password = forms.RegexField(
          label='Old Password',
          widget=ShowPasswordWidget(),
          regex = regex_f.format(MIN=settings.PASSWORD_MIN_LEN, MAX=settings.PASSWORD_MAX_LEN)
                       r'.*$',

          error_messages={'invalid' : 'Password must contain at least 8 characters'
          ' with one upper case letter, one lower case letter,'
          ' one number (0-9) and one special character (!$%^&*?@#-_+=)'})
 
Previously i was using this as:
-----
   old_password = forms.RegexField(
        label='Old Password',
        widget=ShowPasswordWidget(),
        regex=r'^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!$%^&*?@#-_+=])'
                   r'.*$',
 
        error_messages={'invalid' : 'Password must contain at least 8 characters'
         ' with one upper case letter, one lower case letter,'
         ' one number (0-9) and one special character (!$%^&*?@#-_+=)'})

Stephen J. Butler

unread,
May 19, 2016, 3:05:42 AM5/19/16
to django...@googlegroups.com
You can't provide multiple regex's to the RegexField. What you were doing before is exactly equivalent to this:

r'^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!$%^&*?@#-_+=]).*$'

So for my suggestion, just do this:

regex_f=r'^.*(?=.{{{MIN},{MAX}}})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!$%^&*?@#-_+=]).*$'

But the "^.*" and the ".*$" are not needed since RegexField uses RegexValidator, which uses re.search().

Arun S

unread,
May 19, 2016, 3:16:16 AM5/19/16
to Django users

Yes, This works exactly how i intended to.

The Validator Raises error when it does'nt match now.
And i did eliminate the .*$ which dint make much of a difference.

Thanks for the help.

Cheers
Arun.
Reply all
Reply to author
Forward
0 new messages