import re


def update_codelist_urls(text, codelists):
    """
    If the profile defines a codelist, replaces any links to the OCDS codelist with a link to the profile's codelist.
    """

    def replace(match):
        codelist = match.group(2).replace('-', '')
        if any(name for name in codelists if name.lower()[:-4] == codelist):
            return match.group(1) + 'profiles/ppp/latest/{{lang}}/reference/codelists/#' + codelist
        return match.group()

    return re.sub(r'(://standard\.open-contracting\.org/)[^/]+/[^/]+/schema/codelists/#([a-z-]+)', replace, text)