import re


def constrain_regex(string, regex):
    """Validation function checks validity of string for given regex.

    Args:
        string (string): String to be checked
        regex (string): Regular expression

    Returns:
        string: Returns given string

    Raises:
        ValueError: If string doesn't match regex

    """
    regex_email = re.compile(regex, re.IGNORECASE)
    if not regex_email.match(string):
        raise ValueError('Incorrect regex format')
    return string