import re


def compress_sparql(text: str, prefix: str, uri: str) -> str:
    """
    Compress given SPARQL query by replacing all instances of the given uri with the given prefix.

    :param text: SPARQL query to be compressed.
    :param prefix: prefix to use as replace.
    :param uri: uri instance to be replaced.
    :return: compressed SPARQL query.
    """
    bordersremv = lambda matchobj: prefix + ":" + re.sub(f"[<>]|({uri})", "", matchobj.group(0))
    return re.sub(f"<?({uri}).*>?", bordersremv, text)