ts.*_*ts. 1 python regex string
我需要使用简单的通配符匹配两个字符串:
"oh.my.*"比赛"*.my.life","oh.my.goodness"和"*.*.*",但不"in.my.house"
唯一的通配符是*,它替换任何字符的字符串(减去.)
我想过使用fnmatch,但它不接受文件名中的通配符.
我正在使用一些正则表达式的代码 - 更简单的东西会更好,我想:
def notify(self, event, message):
events = []
r = re.compile(event.replace('.','\.').replace('*','[^\.]+'))
for e in self._events:
if r.match(e):
events.append(e)
else:
if e.find('*')>-1:
r2 = re.compile(e.replace('.','\.').replace('*','[^\.]+'))
if r2.match(event):
events.append(e)
for event in events:
for callback in self._events[event]:
callback(self, message)
Run Code Online (Sandbox Code Playgroud)
这应该适合你:
def is_match(a, b):
aa = a.split('.')
bb = b.split('.')
if len(aa) != len(bb): return False
for x, y in zip(aa, bb):
if not (x == y or x == '*' or y == '*'): return False
return True
Run Code Online (Sandbox Code Playgroud)
这个怎么运作:
..*这也算作成功匹配.| 归档时间: |
|
| 查看次数: |
5593 次 |
| 最近记录: |