正则表达式在主机名后打印出行直到分隔符

时间:2014-05-08 10:00:01

标签: python regex list

我的列表如下:

-------------------------------------------------------------------------------------------
www.mydomain.de                         UP                            Thu May  8 09:10:57 2014
              HTTPS                    OK                            Thu May  8 09:10:08 2014
              HTTPS-Cert         OK                            Thu May  8 09:10:55 2014
-------------------------------------------------------------------------------------------
www.someotherdomain.de                         UP                            Thu May  8 09:09:17 2014
              HTTPS                    OK                            Thu May  8 09:09:30 2014
              HTTPS-Cert         OK                            Thu May  8 09:11:10 2014
-------------------------------------------------------------------------------------------
www.somedifferentdomain.at                         UP                            Thu May  8 09:08:47 2014
              HTTPS                    OK                            Thu May  8 09:10:26 2014
              HTTPS-Cert         OK                            Thu May  8 09:11:13 2014
-------------------------------------------------------------------------------------------
www.foobladomain.de                         UP                            Thu May  8 09:09:17 2014
              HTTPS                    OK                            Thu May  8 09:09:30 2014
              HTTPS-Cert         OK                            Thu May  8 09:11:08 2014
-------------------------------------------------------------------------------------------
www.snafudomain.at                         UP                            Thu May  8 09:09:17 2014
              HTTP                     OK                            Thu May  8 09:09:42 2014
              HTTPS                    OK                            Thu May  8 09:10:10 2014
              HTTPS-Cert         OK                            Thu May  8 09:10:09 2014
-------------------------------------------------------------------------------------------
www.lolnotanotherdomain.de                         UP                            Thu May  8 09:06:57 2014
              HTTP                     OK                            Thu May  8 09:11:10 2014
              HTTPS                    OK                            Thu May  8 09:11:16 2014
              HTTPS-Cert         OK                            Thu May  8 09:11:10 2014

我有一个函数,它将主机名作为参数并打印出来:

please enter hostname to search for: www.snafudomain.at
www.snafudomain.at                         UP                            Thu May  8 09:09:17 2014

但我要归档的是主页名后面的以下行打印到分隔符行" -----"我现在的功能看起来像这样:

def getChecks(self,hostname):
    re0 = "%s" % hostname
    mylist = open('myhostlist', 'r')
    for i in mylist:
        if re.findall("^%s" % re0, str(i)):
        print i
        else:
        continue

有一些简单的方法吗?如果有什么不清楚请评论。提前致谢

修改

澄清输出应如下所示:

www.mydomain.de                         UP                            Thu May  8 09:10:57 2014
              HTTPS                    OK                            Thu May  8 09:10:08 2014
              HTTPS-Cert         OK                            Thu May  8 09:10:55 2014
-------------------------------------------------------------------------------------

只想打印出搜索到的域名中的行,直到只有最小的行。

2 个答案:

答案 0 :(得分:1)

import re

def get_checks(hostname):
    pattern = re.compile(r"{}.*?(?=---)".format(re.escape(hostname)), re.S)
    with open("Input.txt") as in_file:
        return re.search(pattern, in_file.read())

print get_checks("www.snafudomain.at").group()

这将返回以www.snafudomain.at开头的所有行,直到找到---。生成的模式将是这样的

www\.snafudomain\.at.*?(?=---)

Online Demo

我们使用re.escape,因为您的主机名中包含.。由于.在正则表达式中具有特殊含义,我们只希望RegEx引擎将.视为文字点。

答案 1 :(得分:1)

根本不使用正则表达式怎么样?

def get_checks(self, hostname):
    record = False
    with open('myhostlist', 'r') as file_h:
        for line in file_h:
            if line.startswith(hostname):
                record = True
                print(line)
            elif line.startswith("---"):
                record = False
                print(line)
            elif record:
                print(line)