def get_regex(regex):
if isinstance(regex, basestring):<EOL><INDENT>return re.compile(regex)<EOL><DEDENT>elif not isinstance(regex, re._pattern_type):<EOL><INDENT>raise TypeError("<STR_LIT>" % (regex,))<EOL><DEDENT>return regex<EOL>
Ensure we have a compiled regular expression object.

    >>> import re
    >>> get_regex('string') # doctest: +ELLIPSIS
    <_sre.SRE_Pattern object at 0x...>
    >>> pattern = re.compile(r'string')
    >>> get_regex(pattern) is pattern
    True
    >>> get_regex(3) # doctest: +ELLIPSIS
    Traceback (most recent call last):
    ...
    TypeError: Invalid regex type: 3