named_field_regex

    Args:
        keypat_tups (list): tuples of (name, pattern) or a string for an unnamed
        pattern

    Returns:
        str: regex

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_regex import *  # NOQA
        >>> keypat_tups = [
        ...     ('name',  r'G\d+'),  # species and 2 numbers
        ...     ('under', r'_'),     # 2 more numbers
        ...     ('id',    r'\d+'),   # 2 more numbers
        ...     ( None,   r'\.'),
        ...     ('ext',   r'\w+'),
        ... ]
        >>> regex = named_field_regex(keypat_tups)
        >>> result = (regex)
        >>> print(result)
        (?P<name>G\d+)(?P<under>_)(?P<id>\d+)(\.)(?P<ext>\w+)

def named_field_regex(keypat_tups):
    """
    named_field_regex

    Args:
        keypat_tups (list): tuples of (name, pattern) or a string for an unnamed
        pattern

    Returns:
        str: regex

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_regex import *  # NOQA
        >>> keypat_tups = [
        ...     ('name',  r'G\d+'),  # species and 2 numbers
        ...     ('under', r'_'),     # 2 more numbers
        ...     ('id',    r'\d+'),   # 2 more numbers
        ...     ( None,   r'\.'),
        ...     ('ext',   r'\w+'),
        ... ]
        >>> regex = named_field_regex(keypat_tups)
        >>> result = (regex)
        >>> print(result)
        (?P<name>G\d+)(?P<under>_)(?P<id>\d+)(\.)(?P<ext>\w+)
    """
    # Allow for unnamed patterns
    keypat_tups_ = [(None, tup) if isinstance(tup, six.string_types) else tup
                    for tup in keypat_tups]
    named_fields = [named_field(key, pat) for key, pat in keypat_tups_]
    regex = ''.join(named_fields)
    return regex