Creating a regular expression in django
Question: New to Django so I'm unsure how to achieve the following...

I have this regular expression

    
    
    "@£$¥èéùìòÇ\fØø\nÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./[0-9]:;<=>\?¡[A-Z]ÄÖÑÜ§¿[a-z]äöñüà\^\{\}\[~\]\|€"
    

I have this function:

    
    
    def validate_GSM_characters(value):
        """ Raise a ValidationError if the value contains any values not within
            the GSM character set.
        """
        msg = u"Invalid characters."
    
        raise ValidationError(msg)
    

At the moment it does nothing, but I need to know how in Django/Python a regex
function that allows me to check value. I did a Google search and can only
seem to find articles relating to model validation.


Answer: Just use the expression with the `re` module:

    
    
    import re
    
    yourrule = re.compile(r"@£$¥èéùìòÇ\fØø\nÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./[0-9]:;<=>\?¡[A-Z]ÄÖÑÜ§¿[a-z]äöñüà\^\{\}\[~\]\|€")
    
    def validate_GSM_characters(value):
        """ Raise a ValidationError if the value contains any values not within
            the GSM character set.
        """
        if not yourrule.search(value):
            msg = u"Invalid characters."
    
            raise ValidationError(msg)
    

You could also just use a
[`RegexValidator`](https://docs.djangoproject.com/en/dev/ref/validators/#regexvalidator);
simply pass it your expression and your message, then use that instead of the
above function.

