import re


def validate_flag(config, submitted_flag):
    """validates a flag against the flags in the challenge config.

    Args:
        config (dict): The normalized challenge config
        flag (string): the flag to validate

    Returns:
        boolean: If the flag was valid
    """
    if config["flag_format_prefix"]:
        if not submitted_flag.startswith(
            config["flag_format_prefix"]
        ) or not submitted_flag.endswith(config["flag_format_suffix"]):
            return False
        submitted_flag = submitted_flag[
            len(config["flag_format_prefix"]) : -len(config["flag_format_suffix"])
        ]

    for flag in config["flags"]:
        if flag["type"] == "text":
            if submitted_flag == flag["flag"]:
                return True

        if flag["type"] == "regex":
            if re.search(flag["flag"], submitted_flag):
                return True

    return False