import re


def is_regex(regex, invert=False):
    """Test that value matches the given regex.

    The regular expression is searched against the value, so a match
    in the middle of the value will succeed.  To specifically match
    the beginning or the whole regex, use anchor characters.  If
    invert is true, then matching the regex will cause the test to
    fail.

    """
    # pylint: disable=unused-argument # args defined by test definition
    rex = re.compile(regex)

    def is_regex_test(conf, path, value):
        match = rex.search(value)
        if invert and match:
            return u'"{0}" matches /{1}/'.format(value, regex)
        if not invert and not match:
            return u'"{0}" does not match /{1}/'.format(value, regex)
        return None

    return is_regex_test