The fnmatch
solutions posted already are certainly recommended for this problem, however, the answer below demonstrates a non-import solution:
def matchs_path(_pattern, _input):
_a, _b = filter(None, _pattern.split('/')), filter(None, _input.split('/'))
while True:
_val, _val2 = next(_a, None), next(_b, None)
if _val is None and _val2 is None:
return True
if _val != '*' and _val != _val2:
return False
if _val == "*":
_to_see = next(_a, None)
if _to_see is None:
return True
while True:
c = next(_b, None)
if c is None:
return True
if c == _to_see:
break
patterns = ['/api/users/*', '/api/account/*', '/new/*/test/here']
data = ['/api/users/add/', '/api/users/edit/1', '/api/users/', '/api/account/view/1', '/api/account/', '/going/to/fail/here', '/new/additional/abc/test/here']
new_results = {i:{c:matchs_path(i, c) for c in data} for i in patterns}
Output:
{
"/api/users/*": {
"/api/users/add/": true,
"/api/users/edit/1": true,
"/api/users/": true,
"/api/account/view/1": false,
"/api/account/": false,
"/going/to/fail/here": false,
"/new/additional/abc/test/here": false
},
"/api/account/*": {
"/api/users/add/": false,
"/api/users/edit/1": false,
"/api/users/": false,
"/api/account/view/1": true,
"/api/account/": true,
"/going/to/fail/here": false,
"/new/additional/abc/test/here": false
},
"/new/*/test/here": {
"/api/users/add/": false,
"/api/users/edit/1": false,
"/api/users/": false,
"/api/account/view/1": false,
"/api/account/": false,
"/going/to/fail/here": false,
"/new/additional/abc/test/here": true
}
}