简体   繁体   中英

Why does IN not evaluate my regular expression?

Using Python 3.x, I am parsing through log files, and on most lines there are one of three key words (INFO, ERROR, or WARN).

I am defining the regular expression to see if a line contains either of these words as:

INFO|ERROR|WARN

I was certain that this was the correct way to go about this, but it does not seem to be working. Does anybody know what I am missing here?

I am checking to see if the regular expression is in the line simply by printing it:

Properties.py
    status = "INFO|ERROR|WARN"

Runner.py
    import properties as p
    import re
    line = "[time stamp] INFO [other information]"
    print(p.status in line)
    line = "[time stamp] ERROR [other information]"
    print(p.status in line)

Output:

False
False

It prints nothing but false.

您需要实际调用一个或多个功能re (未使用in )使用正则表达式引擎:

print(bool(re.search(p.status, line)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM