简体   繁体   English

为什么IN不评估我的正则表达式?

[英]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). 使用Python 3.x,我正在解析日志文件,并且在大多数行中,只有三个关键字(INFO,ERROR或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)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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