简体   繁体   中英

Python (django) regular expression does not work

I have such expression:

^[0-9()|&~ ]*$

It should pass through strings like: '1 & 2 & 3' or '(4&1)|(2&3)'

But when I type string: '1 & 2iuasjhdawiudjsa' , It passes this string. So then I got an error: "Name 'Trueiuasjhdawiudjsa' is not defined".

What's wrong? Why validators.RegexValidator doesn't raise an exception?

Please, help!

Fails in the third validator. If I remove 3rd validator then RegexValidator raises an error, as it should to.

expression = models.CharField(
        blank=True, max_length=225,
        validators=[validators.RegexValidator('^[0-9()|&~ ]*$'),
                    validate_logical_expression,
                    check_logic_operation_positions] # fails here, when try to 
                                                     # check logic
            ]
    )

Resolve this problem:

def check_logic_operation_positions( ...):
    try:
        ...
    except NameError:
        raise ValueError(_('invalid name'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM