import re


def transform_pattern(pattern: str) -> str:
    """Transform shell wildcard expression to equivalent Perl regular expression.
    Return transformed pattern."""

    # Escape special chars
    pattern = re.sub("\\", "\\\\", pattern)
    pattern = re.sub("\/", "\\\/", pattern)
    pattern = re.sub("\^", "\\\^", pattern)
    pattern = re.sub("\$", "\\\$", pattern)
    pattern = re.sub("\(", "\\\(", pattern)
    pattern = re.sub("\)", "\\\)", pattern)
    pattern = re.sub("\[", "\\\[", pattern)
    pattern = re.sub("\]", "\\\]", pattern)
    pattern = re.sub("\{", "\\\{", pattern)
    pattern = re.sub("\}", "\\\}", pattern)
    pattern = re.sub("\.", "\\\.", pattern)
    pattern = re.sub("\,", "\\\,", pattern)
    pattern = re.sub("\|", "\\\|", pattern)
    pattern = re.sub("\+", "\\\+", pattern)
    pattern = re.sub("\!", "\\\!", pattern)

    # Transform ? => (.) and * => (.*)
    pattern = re.sub("\*", "\(\.\*\)", pattern)
    pattern = re.sub("\?", "\(\.\)",   pattern)

    return pattern