import re


def decamelize(string, joiner='_'):
    """Convert a *CamelCase* `string` to a *lower_case* string.

    - The underscores can be changed to a custom `joiner` string.
    """

    def replace(match):
        prefix = (  # Don't prepend the joiner to the beginning of string
            '' if match.group(2) else joiner
        )
        caps = match.group(1).lower()
        follower = match.group(3)
        if not follower:
            return prefix + caps
        if len(caps) == 1:
            return prefix + caps + follower
        return prefix + caps[:-1] + joiner + caps[-1] + follower

    return re.sub('((^[A-Z]+)|[A-Z]+)([a-z])?', replace, string)