I have a db field that holds a regular expression (one unique regex for 
each row in the table). Another row in the table 
(db.steps.readable_response) lists sample strings that should satisfy the 
regex. I'm trying to create a custom validator that tests the regular 
expression against the sample strings at form submission, to highlight 
problems in the regex before it's submitted. But at the moment the 
re.match() is failing even when I'm positive it should pass (I've tested 
them several times using Kiki). Here's the validator class.

class IS_VALID_REGEX(object):
    """
    custom validator to check regex in step definitions against the given
    readable responses.
    """
    def __init__(self):
        self.error_message='Given answers do not satisfy regular 
expression.'


    def __call__(self, value):
        request = current.request
        answers = request.vars.readable_response
        alist = answers.split('|')
        value = value.encode('UTF-8')
        regex = value.encode('string-escape')
        for a in alist:
            if re.match(a.strip(), regex, re.I):
                print a.strip()
                print 'it matched!'
            else:
                print 'answer ', a, ' did not match the regular expression 
provided.'
                print regex
                return (value, self.error_message)
        return (value, None)

The print output show that the correct regex and sample strings are being 
evaluated. So why is it not passing? 

To complicate things a bit, the regex and sample string both include 
unicode characters (polytonic Greek). But elsewhere in the app this doesn't 
seem to have created problems when performing a similar re.match().

Thanks,

Ian

Reply via email to