正则表达式的常用方法

一:贪婪和非贪婪 贪婪: * 尽可能多的去匹配 非贪婪 *? 尽可能少的去匹配content_str = " " " GET ?a=1&b=2 HTTP 1

一:贪婪和非贪婪

贪婪:.* 尽可能多的去匹配

非贪婪 .*? 尽可能少的去匹配

content_str = """
GET /?a=1&b=2 HTTP/1.1
Host: 127.0.0.1:10012
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Sec-Fetch-Site: none
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
"""

同样用search方法为例

request_args = re.search(r"\?(.*)\s",content_str)
print(request_args.group(1))
# 结果
a=1&b=2 HTTP/1.1


request_args = re.search(r"\?(.*?)\s",content_str)
print(request_args.group(1))
# 结果
a=1&b=2

 

二:search方法

python的re模块支持正则表达式,

 

 

 

 

# TODO

 

标签: