我无法在 LogStash 中使用负正则表达式(如 the docs 中所述)
考虑以下正则表达式,它可以正常工作以检测已分配值的字段:
if [remote_ip] =~ /(.+)/ {
mutate { add_tag => ["ip"] }
}
但是,即使字段为空,否定表达式似乎也返回 false:
if [remote_ip] !~ /(.+)/ {
mutate { add_tag => ["no_ip"] }
}
我误解了用法吗?
更新 - 这对我来说是模糊的想法。我的配置文件有问题。如果您的配置文件的其余部分正常,则上述内容应该可以工作。
最佳答案
这对我来说是模糊的想法 - 我的配置文件的其余部分存在问题。
基于 Ben Lim 的例子,我想出了一个更容易测试的输入:
input {
stdin { }
}
filter {
if [message] !~ /(.+)/ {
mutate { add_tag => ["blank_message"] }
}
if [noexist] !~ /(.+)/ {
mutate { add_tag => ["tag_does_not_exist"] }
}
}
output {
stdout {debug => true}
}
空白消息的输出是:
{
"message" => "",
"@version" => "1",
"@timestamp" => "2014-02-27T01:33:19.285Z",
"host" => "benchmark.example.com",
"tags" => [
[0] "blank_message",
[1] "tag_does_not_exist"
]
}
内容为“测试消息”的消息的输出是:
test message
{
"message" => "test message",
"@version" => "1",
"@timestamp" => "2014-02-27T01:33:25.059Z",
"host" => "benchmark.example.com",
"tags" => [
[0] "tag_does_not_exist"
]
}
因此,“负正则表达式”
/(.+)/
仅当该字段为空或该字段不存在时才返回 true。 负正则表达式
/(.*)/
仅当该字段不存在时才返回 true。如果该字段存在(无论是空的还是有值的),则返回值为 false。
关于logstash - logstash 配置中的负正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22032818/