python使用正則表達式解析http url

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')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章