def get_units_regex():
    """Build a compiled regex object."""
    op_keys = sorted(OPERATORS.keys(), key=len, reverse=True)
    unit_keys = sorted(l.UNITS.keys(), key=len, reverse=True)
    symbol_keys = sorted(l.SYMBOLS.keys(), key=len, reverse=True)

    exponent = ur'(?:(?:\^?\-?[0-9%s]*)(?:\ cubed|\ squared)?)(?![a-zA-Z])' % \
               SUPERSCRIPTS

    all_ops = '|'.join([r'%s' % re.escape(i) for i in op_keys])
    all_units = '|'.join([ur'%s' % re.escape(i) for i in unit_keys])
    all_symbols = '|'.join([ur'%s' % re.escape(i) for i in symbol_keys])

    pattern = ur'''

        (?P<prefix>(?:%s)(?![a-zA-Z]))?         # Currencies, mainly
        (?P<value>%s)-?                           # Number
        (?:(?P<operator1>%s)?(?P<unit1>(?:%s)%s)?)    # Operator + Unit (1)
        (?:(?P<operator2>%s)?(?P<unit2>(?:%s)%s)?)    # Operator + Unit (2)
        (?:(?P<operator3>%s)?(?P<unit3>(?:%s)%s)?)    # Operator + Unit (3)
        (?:(?P<operator4>%s)?(?P<unit4>(?:%s)%s)?)    # Operator + Unit (4)

    ''' % tuple([all_symbols, RAN_PATTERN] + 4 * [all_ops, all_units,
                                                  exponent])

    regex = re.compile(pattern, re.VERBOSE | re.IGNORECASE)

    return regex
Build a compiled regex object.