我有以下字符串,应该进行解析:
http_proxy=172.55.30.14:80
https_proxy=Administrator:some_password@172.55.30.27:443
我要从此字符串中提取
protocol = "http" (or "https")
proxy_server = 172.55.30.14
proxy_port = 80
如果设置了密码,还应该初始化变量username和password:
username = Administrator
password = some_password
我有一个使用split方法拆分它的想法:
res = re.split(r':', line)
有什么办法可以更好地做到这一点?
答案 0 :(得分:0)
您可以使用以下代码:
import re
# s="""http_proxy=172.55.30.14:80"""
s="""https_proxy=Administrator:some_password@172.55.30.27:443"""
regex=r"^(?P<xprotocol>http[s]?)_proxy=((?P<xusername>\w+):(?P<xpassword>\w+)@)?(?P<xserver>[\d.]+):(?P<xport>\d+)"
res=re.match(regex,s,re.I)
print("protocol: {}".format(res.group("xprotocol")))
print("proxy_server: {}".format(res.group("xserver")))
print("proxy_port: {}".format(res.group("xport")))
print("username: {}".format(res.group("xusername")))
print("password: {}".format(res.group("xpassword")))
一些注意事项:
– http[s]?
= http
或https
– \w
=一个字母,包括a-> z,A-> Z,0-> 9和_
– +
=一个或多个。因此\w+
=一个或多个字母
– (group)?
=零或一个文本组
– \d+
=一个或多个数字,包括0-> 9