您的位置:首页 > 理论基础 > 计算机网络

python使用正则表达式解析http url

2016-09-22 17:07 726 查看
http Get的原文

/alter?user=abcde&pwd=123456

re.compile(r'/(?P<url_path>alter)\?(?P<query>user=(?P<user>[a-zA-Z]{5,10})&pwd=(?P<passwd>(?:\d|\w){6,}?))$')


step1

(?P<url_path>alter)


找到匹配的http path( alter ),同时命名为url_path

step2

(?P<query>user=(?P<user>[a-zA-Z]{5,10})&pwd=(?P<passwd>(?:\d|\w){6,}?))


匹配query-string .并命名为query

step3

user=(?P<user>[a-zA-Z]{5,10})


匹配user

step4

pwd=(?P<passwd>(?:\d|\w){6,}?)


匹配pwd,其中?为非贪婪模式.

运行结果:

>>> p=re.compile(r'/(?P<url_path>alter)\?(?P<query>user=(?P<user>[a-zA-Z]{5,10})&pwd=(?P<passwd>(?:\d|\w){6,}?))$')
>>> p.match('/alter?user=liujxc&pwd=123456').groups()
('alter', 'user=liujxc&pwd=123456', 'liujxc', '123456')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐