【问题标题】:How to print the line having GET between time range using regex Python如何使用正则表达式 Python 在时间范围之间打印具有 GET 的行
【发布时间】:2019-08-21 12:57:45
【问题描述】:

我的日志文件如下。需要找到具有 GET 并在 2018-07-11 10 和 2018-07-11 11 之间运行的行

2018-07-11 10:12:06 12.115.14.240 "PUT /apng/assembler-2.0/assembler2.php HTTP/1.1" 403 231 "http://littlesvr.ca/apng/history.html" 2018-07-11 10:15:06 202.167.250.99 "GET /apng/images/o_sample.png?142475198 HTTP/1.1" 403 115656 "http://bbs.mydigit.cn/read.php" 2018-07-11 11:16:06 120.115.144.240 "GET /apng/assembler-2.0/assembler2.php HTTP/1.1" 200 231 "http://littlesvr.ca/apng/history.html"

with open(log) as f:
    log = f.read()
    ###if condition to show to get GET
          line = re.search(rx,log)

想要输出 2018-07-11 10:15:06 202.167.250.99 "GET /apng/images/o_sample.png?142475198 HTTP/1.1" 403 115656 "http://bbs.mydigit.cn/read.php"

【问题讨论】:

  • 不要使用log = f.read()。使用for line in f:,然后使用if '"GET ' in line: print(line)

标签: python regex file regex-lookarounds regex-group


【解决方案1】:

我认为正则表达式会起作用:


rx=r"^2018-07-11 1(0|1).*GET"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多