問題以及解
/path/1/b1
/path/{a}/b1
/path/{a}/b1/c
1.輸入一個路徑的值
/path/{正則表達式}/{正則表達式} 設這個字符串爲ST1
2.輸入一個被比較的路徑值
/path/1/b1 :ST2
將第一個字符串ST1 進行split("/")
將第二個字符串ST2 進行split("/")
【“path”,正則1,正則2】列表1
【path,1,b1】列表2
用列表1 和列表2 進行比較 方案用正則
比較結果用 Boolean 值接收
【true , false , false 】
[1,0,0]
相似度 = 【列表1的第一個】*權重 + 【列表1的第二個】*權重
代碼
- 本測試用例使用的測試正則和測試路由長度一樣,如果不一樣的話請自行定製
import re
# 權重係數
weight = [5, 1, 2, 2]
# 測試正則
regex_url = "/path/\d+/\d+"
# 測試路由
test_url = "/path/1/jafa"
regex_list = regex_url.split("/")
test_list = test_url.split("/")
# 正則比較結果
regex_res = []
for regex, test in zip(regex_list, test_list):
finds = re.findall(regex, test)
a = (1 if finds else 0)
regex_res.append(a)
sum = 0
for w, i in zip(regex_res, weight):
sum += w * i
print("sum :", sum)