Skip to main content
 首页 » 编程设计

python之python的正则表达式

2024年12月31日28powertoolsteam

我在用 python 编写正则表达式时遇到了困难。 例如我得到以下权利

"GET /images/launch-logo.gif HTTP/1.0" 220 1839 

匹配

"(\S+) (\S+)\s*(\S*)" (\d{3}) (\S+) 

但是我仍然需要将以下情况全部包括在内

  1. "GET/history/history.html hqpao/hqpao_home.html HTTP/1.0"200 1502
  2. "GET/shuttle/missions/missions.html 航天飞机从 肯尼迪航天中心 HTTP/1.0"200 8677
  3. "GET/finger @net.com HTTP/1.0"404 -

显然我应该更改表达式的粗体部分

"(\S+) (\S+)\s*(\S*)" (\d{3}) (\S+)

But how should I change it. I have one approach in mind which is change the bold part to

[\s |(\s*)(\S+) |(\S+)(12) |(\S+)] 

其中第 2、3、4 个表达式是我需要处理的 (1)、(2)、(3) 种额外情况。

但是我的表情不起作用。我对正则表达式有什么误解,因为我只是逐个处理它。

请您参考如下方法:

这可能有点乱,但它有效:

\"(\S+) (\S+[\s\w\.\@]*)\s*(\S*)\"\s?(\d{3})\s(\S+)* 

您可以在 Regexr 上使用它。 Regexr Shared Link