zoukankan      html  css  js  c++  java
  • 正则表达式的常用方法

    一:贪婪和非贪婪

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

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

    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

     

  • 相关阅读:
    最小路径
    零钱兑换
    硬币
    三步问题
    区域和检索
    除数博弈
    URI和URL的辨别
    交叉编译OpenMP
    牛客挑战赛44D-数列的和
    CF1408H. Rainbow Triples
  • 原文地址:https://www.cnblogs.com/meloncodezhang/p/12807229.html
Copyright © 2011-2022 走看看