Python正则表达式应该从文件中返回几行时不返回任何内容
我有一个快速的python程序Python正则表达式应该从文件中返回几行时不返回任何内容,python,regex,Python,Regex,我有一个快速的python程序 import logging import logging.handlers import re with open("sampleData2.log", "r") as ins: array = [] for line in ins: if re.match("app web\.1.-.*", line): print line + "\n\n\n\n"; 如果我是正确的,它应该返回包含app web.
import logging
import logging.handlers
import re
with open("sampleData2.log", "r") as ins:
array = []
for line in ins:
if re.match("app web\.1.-.*", line):
print line + "\n\n\n\n";
如果我是正确的,它应该返回包含app web.1的任何行,但在我运行程序时不会返回任何内容。如果我删除正则表达式,我确认文件实际上正在输出所有内容。下面是文件数据的示例
109 <190>1 2015-01-22T19:43:18.632927+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd app web.1 - - CRASdsafH fatal
109 <190>1 2015-01-22T19:43:18.632932+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd app web.1 - - Test this errpr
162 <190>1 2015-01-22T19:43:18.633277+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd app web.1 - - Rendered welcome/index.html.erb within layouts/application (0.0ms)
Connection from SyslogdProtocol #12 on 5144
342 <158>1 2015-01-22T19:43:18.622382+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd heroku router - - at=info method=GET path="/assets/application-8474e4f266741613a6d5486dc2913241.js" host=####.herokuapp.com request_id=5b4d4491-8192-4f16-a407-c9867c8b8ac3 fwd="209.36.39.50" dyno=web.1 connect=2ms service=53ms status=200 bytes=39915
340 <158>1 2015-01-22T19:43:18.744631+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd heroku router - - at=info method=GET path="/assets/application-7ea
109 1 2015-01-22T19:43:18.632927+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd应用程序web.1--死机
109 1 2015-01-22T19:43:18.632932+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd应用程序web.1--测试此错误
162 1 2015-01-22T19:43:18.633277+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd应用程序web.1--布局/应用程序中呈现的欢迎/索引.html.erb(0.0ms)
从5144上的SyslogdProtocol#12连接
34212015-01-22T19:43:18.622382+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd heroku路由器--at=info-method=GET-path=“/assets/application-8474F266741613AD5486DC2913241.js”主机=####herokuapp.com请求id=5b4d4491-8192-4f16-a407-C9867C8BAC3 fwd=“209.36.39.50”网络连接状态=200字节=535
340 1 2015-01-22T19:43:18.744631+00:00 d.eae4693f-68e0-404f-a798-90943699b1dd heroku路由器--at=info-method=GET-path=“/assets/application-7ea
有什么想法吗?使用
search
而不是match
match
只在字符串开头查找要匹配的正则表达式。基本上:
# if re.match("app web\.1.-.*", line):
if re.search("app web\.1.-.*", line):
另请参见使用
搜索
而不是匹配
匹配
仅在字符串开头查找要匹配的正则表达式。基本上:
# if re.match("app web\.1.-.*", line):
if re.search("app web\.1.-.*", line):
还可以看到