将某个单词后的所有内容存储在一行、一个Regex列表中

2024-07-06 03:56:59 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有一句台词

unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/NASA-logosmall.gif HTTP/1.0" 200 786

我想把HTTP/1.0(这两个数字)之后的所有内容都存储到列表中,我该如何使用regex来实现这一点?我看了他们的文件,但他们有点迷惑我


Tags: http内容列表getnet数字gifjul
3条回答

您可以使用regex101来构造适合您需要的正则表达式

对于您的特定示例,以下RE将起作用:

HTTP\/1.0.(.*$)

说明:

在组中捕获HTTP 1.0"之后的所有内容

提供输出:

` 200 786`

您不需要regex,可以使用内置的str方法。例如

s = 'unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/NASA-logosmall.gif HTTP/1.0" 200 786'
data = s.partition('HTTP/1.0" ')
nums = data[2].split()
print(nums)

输出

['200', '786']

你也可以用.split()代替.partition(),但我认为.partition()在这里更自然。请注意,nums中存储的数字是字符串,因此如果需要对它们进行算术运算,则需要添加转换步骤

下面是一个使用.split()而不是.partition()将数字字符串转换为整数的示例

data = s.split('HTTP/1.0"')
nums = [int(u) for u in data[1].split()]
print(nums)

输出

[200, 786]
import re
text = 'unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/NASA-logosmall.gif HTTP/1.0" 200 786'
regex = r'HTTP/1.0".*$'
match = re.search(regex, text)
list_with_numbers = match.groups()[0].split()

相关问题 更多 >