'Convert AdBlock rule to a regular expression.'

@classmethod
def rule_to_regex(cls, rule):

    if (not rule):
        raise ValueError('Invalid    rule')
    rule = re.sub('([.$+?{}()\\[\\]\\\\])', '\\\\\\1', rule)
    rule = rule.replace('^', '(?:[^\\w\\d_\\-.%]|$)')
    rule = rule.replace('*', '.*')
    if (rule[(-1)] == '|'):
        rule = (rule[:(-1)] + '$')
    if (rule[:2] == '||'):
        if (len(rule) > 2):
            rule = ('^(?:[^:/?#]+:)?(?://(?:[^/?#]*\\.)?)?' + rule[2:])
    elif (rule[0] == '|'):
        rule = ('^' + rule[1:])
    rule = re.sub('(\\|)[^$]', '\\|', rule)
    return rule
