[Python] Python runs regex on variable but not on file with same content

Discussão em 'Python' iniciado por Stack, Agosto 30, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a python (2.7) script to parse some logs from a java application using regex. I used http://pythex.org/ to help test the patterns and they work there with a reduced log sample just fine.

    Once I do the same on my script it works if I put some of the log in a variable, but wont work if I point it to a file.

    Here is the code

    import re
    regex_sql_java_error = "\[use.(.*?)\]\nThread:.{9}(GENERAL|LOADER).{17}(ERROR(.*?)\n)"
    logfile = open('example_files/Log_file.txt', 'r')
    data = logfile.read()
    logfile.close()

    filtered = re.finditer(regex_sql_java_error, data, re.DOTALL | re.MULTILINE)
    if filtered:
    for item in filtered:
    print item.group(0)


    The logfile I am using is a measly 1MB file.

    I can't imagine the pattern being the issue, but heres a sample of the log file that matched just fine on pythex.org

    Thread: 5624 LOADER 08:26:37.078 INFO executeDdlStatements:
    [use ADMINI;, SOME BROKEN SQL HERE;]
    Thread: 5624 LOADER 08:26:37.086 ERROR 'executeDdlStatements' command failed with the error: Table 'ADMININTT' doesn't exist
    RANDOM JAVA STUFF


    Link to it on pythex

    I've been bashing my head on this for a couple days, read a bunch of docs, but I cant figure out what I am doing wrong.

    Hopefully its something really stupid Ill be able to laugh about later.

    Anyway, if anybody can point me in the right direction I'd really appreciate.

    Continue reading...

Compartilhe esta Página