简体   繁体   English

Python正则表达式:用户输入多个搜索词

[英]Python Regex: User Inputs Multiple Search Terms

What my code is suppose to do is take in user input search terms then iterate through a tcp dump file and find every instance of that term by packet. 我的代码应该做的是接受用户输入的搜索词,然后遍历tcp转储文件,并按包查找该词的每个实例。 The src IP acts as the header to each packet in my output. src IP充当我输出中每个数据包的标头。

So I am having an issue with the fileIn being seemingly erased when it iterates through the first term. 所以我在遍历第一个术语时似乎删除了fileIn问题。 So when the program goes to look at the second user input search term it obviously can't find anything. 因此,当程序查看第二个用户输入的搜索词时,显然找不到任何东西。 Here is what I have: 这是我所拥有的:

import re
searchTerms = []

fileIn = open('ascii_dump.txt', 'r')

while True:
    userTerm = input("Enter the search terms (End to stop): ")
    if userTerm == 'End':
        break
    else:
        searchTerms.append(userTerm)

ipPattern = re.compile(r'((?:\d{1,3}\.){3}\d{1,3})')

x = 0

while True:
    print("Search Term is:", searchTerms[x])
    for line in fileIn:
        ipMatch = ipPattern.search(line)
        userPattern = re.compile(searchTerms[x])
        userMatch = userPattern.search(line)

        if ipMatch is not None:
            print(ipMatch.group())

        if userMatch is not None:
            print(userMatch.group())
    x += 1
    if x >= len(searchTerms):
       break

This happens because you opened the file object as an iterator which is consumed in the first past through the for loop. 发生这种情况是因为您打开了文件对象作为迭代器,而迭代器是通过for循环使用的。

During the second time through the loop, the for line in fileIn will not be evaluated since the iterator fileIn has already been consumed. 在第二遍循环中,由于迭代器fileIn已被使用,因此不会评估for line in fileInfor line in fileIn

A quick fix is to do this: 一个快速的解决方法是:

lines = open('ascii_dump.txt', 'r').readlines()

then in your for loop, change the for line in fileIn to: 然后在for循环中,将for line in fileInfor line in fileIn更改for line in fileIn

for line in lines:

Having said this, you should rewrite your code to do all regex matches in a single pass using the regex or operator. 话虽如此,您应该使用regex或运算符重写代码以一次完成所有regex匹配。

You need to "rewind" the file after the for line in fileIn loop: 您需要for line in fileIn循环中的for line in fileIn之后“倒回”文件:

...
fileIn.seek(0);
x += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM