def build_regex(pattern, pattern_name=None, **kwargs):
    """
    Return regex string as a named capture group.
    See: https://tonysyu.github.io/readable-regular-expressions-in-python.html
    """
    pattern = pattern.format(**kwargs)

    if pattern_name is not None:
        return r'(?P<{name}>{pattern})'.format(name=pattern_name, pattern=pattern)

    return pattern