from typing import Optional
import re


def find_urls_by_ext(text: str, ext: str, commanurls: Optional[bool] = True) -> list:
    """find urls with specific extension from a text string using regular expressions

    Args:
        text (str): text to search for
        ext (str): extension to use
        commanurls (Optional[bool], optional): sort out unique urls only. Defaults to True.

    Returns:
        list: list of urls found
    """
    regex = re.compile(rf"(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-]\.{ext})")
    # regex = re.compile(rf"https?://[a-zA-Z0-9-._%/]*\.{ext}")
    matches = re.findall(regex, text)
    matches = map(lambda x: f"{x[0]}://{x[1]}{x[2]}", matches)

    if commanurls:
        return list(matches)

    urls = []
    for match in matches:
        if match not in urls:
            urls.append(match)

    return urls