匹配两个通配符字符串

时间:2015-09-10 23:27:03

标签: python regex string

我刚学会了如何将字符串与来自(非常有帮助的)Python wildcard search in string

的通配符匹配

现在我正在尝试匹配两个都有通配符的字符串。

string1 = "spotify.us.*.uk"
string2 = "spotify.*.co.uk"

这两个字符串应该匹配。使用*将用作通配符。我的在线研究显示无解决方案。我到目前为止(不工作):

import re

string1 = "spotify.us.*.uk"
string2 = "spotify.*.co.uk"
r1 = string1.replace("*", ".*")
r2 = string2.replace("*", ".*")
regex1 = re.compile('.*'+r1)
regex2 = re.compile('.*'+r2)

matches = re.search(regex1, regex2)

我使用相同的概念来匹配stringregex这是有效的。但是在这两个字符串都有通配符的情况下它不起作用。任何帮助将不胜感激。

0 个答案:

没有答案
相关问题