Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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 来自Windows系统进程的正则表达式匹配日期_Python_Regex_Windows_Netstat - Fatal编程技术网

Python 来自Windows系统进程的正则表达式匹配日期

Python 来自Windows系统进程的正则表达式匹配日期,python,regex,windows,netstat,Python,Regex,Windows,Netstat,我想使用Windows的命令net statistics server并使用模式为:(\d+-\d+-\d+:\d+:\d+:\d+) def sysCmd(string): try: res = subprocess.Popen(string) return res except: return "NULL - Command Error" loginTime = sysCmd ("net statistics server ") # windows command

我想使用Windows的命令net statistics server并使用模式为:(\d+-\d+-\d+:\d+:\d+:\d+)

def sysCmd(string):
try:
    res = subprocess.Popen(string)
    return res
except:
    return "NULL - Command Error"

loginTime = sysCmd ("net statistics server ") # windows command
loginRex = "(\d+-\d+-\d+ \d+:\d+:\d+)"
loginMatch = re.search(loginRex, str(loginTime))
print (loginMatch.group(0))
我得到一个错误: AttributeError:“非类型”对象没有属性“组”

正则表达式在正则表达式测试中似乎可以很好地利用这个windows命令提供的输出。在这里,经过这么多的尝试,它仍然没有失败

我做错了什么


p、 我使用的是Python3,我是Python2.7的用户,所以我不知道正则表达式中是否存在差异,但我不知道“-”在其中的含义。对我来说,有效的方法是:

loginRex = "(\d?\d?\d+:\d+:\d+)"
这将与1981:05:17和81:05:17相匹配。如果不需要后者,只需删除“?”。你能试试这个吗


干杯

我意识到问题实际上存在于我调用子流程的函数中。它必须配备旗帜

res = subprocess.Popen(string)
return res
现在是

res = subprocess.Popen(string, shell=False, stdout=subprocess.PIPE)
return str(res.communicate())

该死的愚蠢错误!;)

我想你在loginRex之后有打字错误?无论如何,如果你得到的是AttributeError,这不应该是你的问题,但只是一个你可能想要纠正的输入错误。你能打印其中一个登录时间让我们看看它们是什么样子吗?对于那些不使用windows的人来说,可能有一个答案。干杯Jeje,只是没有打印测试答案,旗帜可能发生在任何人身上;)。不管怎样,很高兴你解决了这个问题!