Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用正则表达式删除python中的重复字符串_Python_Regex - Fatal编程技术网

使用正则表达式删除python中的重复字符串

使用正则表达式删除python中的重复字符串,python,regex,Python,Regex,我有一个.txt文件,其中生成了大量Snort警报。我想搜索此文件并删除重复的警报,并且只保留每个警报中的一个。到目前为止,我正在使用以下代码: with open('SnortReportFinal', 'r') as f: file_lines = f.readlines() cont_lines = [] for line in range(len(file_lines)): if re.search('\d:\d+:\d+', file_lines[line])

我有一个.txt文件,其中生成了大量Snort警报。我想搜索此文件并删除重复的警报,并且只保留每个警报中的一个。到目前为止,我正在使用以下代码:

with open('SnortReportFinal', 'r') as f:
    file_lines = f.readlines()

cont_lines = []
for line in range(len(file_lines)):
        if re.search('\d:\d+:\d+', file_lines[line]):
        cont_lines.append(line)

for idx in cont_lines[1:]: # skip one instance of the string
    file_lines[idx] = "" # replace all others

with open('SnortReportFinal', 'w') as f:
    f.writelines(file_lines)
正则表达式匹配我正在搜索的字符串,即1:234:5,如果它找到同一字符串的多个实例,我希望它删除它们并只保留一个。这不起作用,因为将删除所有其他字符串,并且只保留表达式匹配的一个字符串

文件包含如下文本:

[1:368:6] ICMP PING BSDtype [**]
[1:368:6] ICMP PING BSDtype [**]
[1:368:6] ICMP PING BSDtype [**]
[1:368:6] ICMP PING BSDtype [**]
其中部分[1:368:6]可以是数字的变体,即[1:5476:5]

我希望我的预期输出仅为:

[1:368:6] ICMP PING BSDtype [**]
[1:563:2] ICMP PING BSDtype [**]

其余的字符串被删除,我所说的rest是指数字之间的差异很好,但不是重复的数字。

看起来你真的不需要正则表达式。要删除重复项,只需执行以下操作:

alerts = set(f.readlines())
这会将文件中的行列表转换为一个集合,从而删除重复项。从这里,您可以直接将集合写回文本文件

或者,您可以直接调用file对象上的set,正如Padraic Cunningham在注释中指出的:

alerts = set(f)

看起来你真的不需要正则表达式。要删除重复项,只需执行以下操作:

alerts = set(f.readlines())
这会将文件中的行列表转换为一个集合,从而删除重复项。从这里,您可以直接将集合写回文本文件

或者,您可以直接调用file对象上的set,正如Padraic Cunningham在注释中指出的:

alerts = set(f)

您不需要
regex
您可以使用
set

seen=set(i.strip() for i in open('infile.txt'))
例如:

>>> s="""[1:368:6] ICMP PING BSDtype [**]
... [1:368:6] ICMP PING BSDtype [**]
... [1:368:6] ICMP PING BSDtype [**]
... [1:368:6] ICMP PING BSDtype [**]
... [1:563:2] ICMP PING BSDtype [**]"""
>>> set(s.split('\n'))
set(['[1:563:2] ICMP PING BSDtype [**]', '[1:368:6] ICMP PING BSDtype [**]'])

您不需要
regex
您可以使用
set

seen=set(i.strip() for i in open('infile.txt'))
例如:

>>> s="""[1:368:6] ICMP PING BSDtype [**]
... [1:368:6] ICMP PING BSDtype [**]
... [1:368:6] ICMP PING BSDtype [**]
... [1:368:6] ICMP PING BSDtype [**]
... [1:563:2] ICMP PING BSDtype [**]"""
>>> set(s.split('\n'))
set(['[1:563:2] ICMP PING BSDtype [**]', '[1:368:6] ICMP PING BSDtype [**]'])

警报在文件中的顺序是否重要?否警报可以是任何顺序。为什么要使用正则表达式?是否还有其他行?是否重要警报在文件中的顺序?否警报可以是任何顺序。为什么要使用正则表达式?是否有其他行?除非映射,否则可能会失败(str.rstrip,也可以在文件上调用setobject@PadraicCunningham如果没有
rstrip()
的话,它会失败的唯一原因是空格之间存在差异;计算机生成的输出不应该是问题。+1用于调用
set()
直接在文件对象上though@wnnmaw,如果末尾有一个重复的行而没有换行符等,它将无法捕获它。这可能会失败,除非您映射(str.rstrip),您还可以调用文件上的setobject@PadraicCunningham它在没有
rstrip()的情况下失败的唯一原因
如果空格中存在差异,则为空;计算机生成的输出不应成为问题。+1用于直接在文件对象上调用
set()
though@wnnmaw,如果末尾有一个重复的行,而没有换行符等,它将无法捕获它。