import re


def regex_replace(
    base: str,
    regex: str,
    replace: str = "",
    case_sensitive: bool = False,
    count: int = 0,
) -> str:
    """Replace matching regex pattern"""
    if not isinstance(base, str):
        # Raise an error instead?
        return base

    args = {
        "pattern": regex,  # re.escape
        "repl": replace,
        "string": base,
        "count": count,
    }
    if not case_sensitive:
        args["flags"] = re.IGNORECASE

    return re.sub(**args)    # type: ignore