正则表达式如何

时间:2013-12-03 12:52:20

标签: python regex

很难解释清楚,请参阅以下代码。基本上我想要一个简单的逻辑: 1.如果模式匹配,则返回repl。 2.如果repl包含reference,则用匹配替换引用。

patterns = [ 
 #pattern, repl
 (r'utm_source', r'ad'),
 (r'utm_source=([\w]+)', r'ad:\1'),
 (r'mayuki', r'visit'),
 (r'showProduct', r'product'),
 (r'CrShopCar',   r'cart'),
 (r'CrShopCar03', r'payment'),
]

def parse(url):
    # for each pattern, if match
    # return it's repl
    # the following will failed 
    actions = []
    for pattern, repl in patterns:
        if re.findall(pattern, url):
            actions.append(re.sub(pattern, repl, url))
    return actions



assert parse('http://www.mayuki.com.tw') == ["visit"]
assert parse('www.mayuki.com.tw/showProduct=123') == ["visit", "product"]
assert parse('www.mayuki.com.tw/?utm_source=yahoo') == ["ad", "ad:yahoo"]

1 个答案:

答案 0 :(得分:3)

我猜最后一个应该返回['ad', 'ad:yahoo', 'visit']。鉴于此,

for pattern, repl in patterns:
    m = re.search(pattern, url)
    if m:
        actions.append(m.expand(repl))

docs:expand