[英]Python Regex expression is failing - can't figure out why
I'm using: 我正在使用:
re.search(r'^[A-Za-z0-9\:\(\) ]+\[[A-Za-z0-9\.]+\:[0-9]+\]$', log)
To search a log that looks like this: 要搜索如下所示的日志:
"Logged at Thursday, March 20, 2014 20:48:03
FileVersion: 3.5.0.44954
ProductVersion: 3.5.0.44954
LogFile: Game.log
Exception Code: FATAL_ERROR
Exception Addr:
Exception Module:
Exception Description: FATAL_ERROR,
Memory in use: 462.7MB
Debug Status:
Out of Memory: 0
Call Stack Trace:
10) CSystem::FatalError() [systemwin32.cpp:1267]
9) CFatalError() [isystem.h:1528]
When I test the expression in Sublime Text/Notepad++, it succeeds. 当我在Sublime Text / Notepad ++中测试表达式时,它会成功。 But when I run it as shown above in Python, I get nothing back.
但是当我在Python中运行它时,我什么也得不回来。
Any ideas? 有任何想法吗?
Use re.M for multiline regex matching. 使用re.M进行多行正则表达式匹配。
re.search(r'^[A-Za-z0-9\:\(\) ]+\[[A-Za-z0-9\.]+\:[0-9]+\](?:[\n\r]|\Z)', log, re.M)
^^^^^^^^
Also, this regex is checking for [\\n\\r]
or \\Z
at the end of regex instead of $
. 此外,这个正则表达式是在正则表达式结束时检查
[\\n\\r]
或\\Z
而不是$
。 FYI, \\Z
means end of input string. FYI,
\\Z
表示输入字符串的结尾。
Elaborating on Sabuj Hassan's answer, the problem is simply that Python is not recognizing the end of the line as a terminal. 在阐述Sabuj Hassan的答案时,问题很简单,就是Python没有认识到该行的结尾是一个终端。 This is because you are not using the
MUTILINE
flag , re.M
. 这是因为你没有使用
MUTILINE
标志 , re.M
。
Without this flag Python does not assign any special meaning to \\n
characters and thus $
will only match the very end of the string. 没有这个标志,Python不会为
\\n
字符赋予任何特殊含义,因此$
只会匹配字符串的最后一端。 By including the re.M
flag Python automatically adds terminals before all \\n
characters, thus allowing $
to match both end-of-line and end-of-string. 通过包含
re.M
标志,Python会在所有\\n
字符之前自动添加终端,从而允许$
匹配行尾和字符串结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.