CogitoErgoSum
CogitoErgoSum

Reputation: 2907

Python Regex returning nothing when it should return several lines from file

I have a quick python program

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";

It should return any line containing app web.1 if i'm correct, yet nothing returns when I run the program. I confirmed the file is actually outputting all contents if I remove the regex. Below is a sample of the file data

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

any thoughts?

Upvotes: 0

Views: 49

Answers (1)

rchang
rchang

Reputation: 5236

Use search instead of match. match only looks for the regular expression to match at the beginning of the string. Basically:

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

Also see the upstream documenation on re.search and re.match

Upvotes: 5

Related Questions