正则表达式的常用方法
一:贪婪和非贪婪
贪婪:.* 尽可能多的去匹配
非贪婪 .*? 尽可能少的去匹配
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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步