我試圖用一個通用名稱來匹配域名。當我查看證書時,我看到通用名稱是"*.example.com"
。可能的領域可能是:URL通用名稱匹配 - Python
www.example.com # A match the leftmost label of *.example.com
example.com # A match the leftmost label of *.example.com
hello.example.com # A match for the leftmost label of *.example.com
foo.bar.example.com # Not a match for the leftmost label of
*.*.* # Not a match for the leftmost label of *.example.com
www.*.com # Not a match for the leftmost label of *.example.com
* .example.com的
我試圖創建下面的正則表達式:
import re
common_name = "*.example.com"
regex = common_name.replace('*','.*') + '$'
url = "foo.bar.example.com"
if re.match(regex, url):
print "yes"
else:
print "no"
什麼是錯在我的正則表達式?
您還需要事先對字符串進行轉義('re.escape()'),否則所有其他'.'將被視爲正則表達式通配符 – Felk