所以我有一个脚本可以解析tomcat访问日志,然后用正则表达式模式匹配每一行。现在如果我通过命令行运行正则表达式(在终端上使用python3调用python3解释器)在该日志的相应行上,我得到一个匹配但是如果我使用相同的正则表达式运行脚本我得到nada。不知道为什么。
命令行输出:
>>> obj = re.compile(r'''(?P<bytes_32>[a-z/\?=0-9]+)
... (?P<PeerS>x0a5a906a)
... (?P<MsTimestamp>Z([a-f0-9]+|[0-9]+))
... (?P<RemotePort>AB([0-9]+))
... (?P<RemoteIP>A([0-9\.]+){4})
... (?P<PeerIP>C([0-9\.]+){4})
... (?P<PeerPort>D[0-9]+)
... (?P<LocalIP>E([0-9\.]+){4})
... (?P<LocalPort>F[0-9]+)
... (?P<HostHeader>w[a-z\._0-9\-]+)
... (?P<ContentType>K[a-z/]+)
... (?P<Method>m[A-Z]+)
... ''', re.VERBOSE)
>>>
>>> string = "0a5a906a57f41c6c00002ee0ffffffff/abcd/api/v1//examples/1348646x0a5a906aZ726AB46151A11.47.166.200C11.47.166.200D46151E11.47.166.200F8443wexamples-api-nobody.corp.myself.comKapplication/jsonmGET"
>>> match = obj.match(string)
>>> print(match)
<_sre.SRE_Match object; span=(0, 195), match='0a5a906a57f41c6c00002ee0ffffffff/screenr/api/v1//>
通过脚本输出:
line,obj: 0a5a906a57f41c6c00002ee0ffffffff/abcd/api/v1//examples/1348646x0a5a906aZ726AB46151A11.47.166.200C11.47.166.200D46151E11.47.166.200F8443wexamples-api-nobody.corp.myself.comKapplication/jsonmGET
re.compile('(?P<bytes_32>[a-z/\\?=0-9]+)\n (?P<PeerS>x0a5a906a)\n (?P<MsTimestamp>Z([a-f0-9]+|[0-9]+))\n (?P<RemotePort>AB([0-9]+))\n , re.VERBOSE)
match is: None
当我对两者使用相同的输入和模式时,为什么解释器和脚本有区别?