在Python

时间:2017-04-19 22:14:01

标签: python regex

我试图匹配gnmap文件中的以下行:

23 /开/ TCP //远程登录

我希望只输出端口号。有时候港口是23个,其中2323或6000,或其他数字。

我已尝试过此代码,但正则表达式无法正常匹配:

import re

class REMatcher(object):
    def __init__(self, matchstring):
        self.matchstring = matchstring

    def match(self,regexp):
        self.rematch = re.match(regexp, self.matchstring)
        return bool(self.rematch)

    def group(self,i):
        return self.rematch.group(i)

gnmap_file = "telnet.gnmap"

with open(gnmap_file, "r") as f:
    lines = f.readlines()
    for line in lines:
        m = REMatcher(line)
        if m.match("([0-9])*\/open\/tcp\/\/telnet"):
            print m.group(1)

我想我在正则表达式中遗漏了一些东西。任何帮助将不胜感激。

0 个答案:

没有答案