import re


def to_regex(regex, flags=0):
    """
    Given a string, this function returns a new re.RegexObject.
    Given a re.RegexObject, this function just returns the same object.

    :type  regex: string|re.RegexObject
    :param regex: A regex or a re.RegexObject
    :type  flags: int
    :param flags: See Python's re.compile().
    :rtype:  re.RegexObject
    :return: The Python regex object.
    """
    if regex is None:
        raise TypeError('None can not be cast to re.RegexObject')
    if hasattr(regex, 'match'):
        return regex
    return re.compile(regex, flags)