import re


def WildcardSearchToRegularExpression(
    value,
    as_string=False,
):
    """Converts from a wildcard expression (supporting '*' and '?') to a regular expression)"""

    value = re.escape(value)

    for source, dest in [(r"\*", ".*"), (r"\?", ".")]:
        value = value.replace(source, dest)

    value = "^{}$".format(value)

    if as_string:
        return value

    return re.compile(value)