python中列表中的re.findall

2024-07-05 11:37:49 发布

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

我有一个清单如下

sample_text = ['199.72.81.55 -- [01/Jul/1995:00:00:01 -0400] "Get /histpry/appollo/HTTP/1.0" 200 6245',
    'unicomp6.unicomp.net -- [01/Jul/1995:00:00:06 -0400] "Get /shuttle/countdown/HTTP/1.0" 200 3985', 
    '199.120.110.21 -- [01/Jul/1995:00:00:01 -0400] "Get /histpry/appollo/HTTP/1.0" 200 6245',
    'burger.letters.com -- [01/Jul/1995:00:00:06 -0400] "Get /shuttle/countdown/HTTP/1.0" 200 3985', 
    '205.172.11.25 -- [01/Jul/1995:00:00:01 -0400] "Get /histpry/appollo/HTTP/1.0" 200 6245']

我需要把所有的主机名都列在一个列表中。预期结果如下

['199.72.81.55', 'unicomp6.unicomp.net', '199.120.110.21', 'burger.letters.com', '205.172.11.25']

我的代码是:

for i in range(0, len(sample_text)):
    s=sample_text[i]
    host.append(re.findall('[\d]*[.][\d]*[.][\d]*[.][\d]*|[a-z0-9]*[.][a-z]*[.][a-z]*', s))
print(host)

我的输出:

[['199.72.81.55'], ['unicomp6.unicomp.net'], ['199.120.110.21'], ['burger.letters.com'], ['205.172.11.25']]

我该如何解决这个问题


Tags: sampletextcomhttpgetnetjulburger
3条回答

re.findall()返回字符串列表

文件:https://docs.python.org/3/library/re.html#re.findall

.append将列表作为单个项目添加到新列表中

尝试:

host.extend(

文件:https://docs.python.org/3/tutorial/datastructures.html

您可以轻松展平host

host = []
for i in range(0, len(sample_text)):
    s=sample_text[i]
    host += re.findall('[\d]*[.][\d]*[.][\d]*[.][\d]*|[a-z0-9]*[.][a-z]*[.][a-z]*', s)
print(host)

输出:

['199.72.81.55', 'unicomp6.unicomp.net', '199.120.110.21', 'burger.letters.com', '205.172.11.25']

不使用正则表达式,您只需str.split'--'上执行第一部分

>>> [i.split('--')[0].strip() for i in sample_text]
['199.72.81.55', 'unicomp6.unicomp.net', '199.120.110.21', 'burger.letters.com', '205.172.11.25']

类似的想法,但使用正则表达式

>>> import re
>>> [re.match(r'(.*) -- .*', i).group(1) for i in sample_text]
['199.72.81.55', 'unicomp6.unicomp.net', '199.120.110.21', 'burger.letters.com', '205.172.11.25']

在这两种情况下,您都可以使用列表理解来替换for循环

相关问题 更多 >