Python Regex在从文件返回多行时不返回任何内容

时间:2015-01-22 20:02:41

标签: 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.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

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

使用search代替matchmatch仅查找在字符串开头匹配的正则表达式。基本上是:

# if re.match("app web\.1.-.*", line):
if re.search("app web\.1.-.*", line):

另见upstream documenation on re.search and re.match