Skip to content

Text

ocx_mirror_sdk.text.extract_urls

extract_urls(text: str, *, pattern: str | None = None) -> list[str]

Extract URLs from text, optionally filtering by pattern.

Returns a deduplicated list of URLs in the order they first appear. If pattern is given, only URLs matching re.search(pattern, url) are included.

Source code in src/ocx_mirror_sdk/text.py
def extract_urls(text: str, *, pattern: str | None = None) -> list[str]:
    """Extract URLs from *text*, optionally filtering by *pattern*.

    Returns a deduplicated list of URLs in the order they first appear.
    If *pattern* is given, only URLs matching ``re.search(pattern, url)``
    are included.
    """
    seen: set[str] = set()
    urls: list[str] = []
    for match in _URL_RE.finditer(text):
        url = match.group(0).rstrip(".,;:")
        if url in seen:
            continue
        if pattern and not re.search(pattern, url):
            continue
        seen.add(url)
        urls.append(url)
    return urls