我无法获得在LogStash中工作的负regexp表达式(如医生们中所述)
考虑以下正正则表达式,它可以正确地检测已分配值的字段:
if [remote_ip] =~ /(.+)/ {
mutate { add_tag => ["ip"] }
}
但是,即使字段为空,否定式表达式似乎也返回false:
if [remote_ip] !~ /(.+)/ {
mutate { add_tag => ["no_ip"] }
}
我是不是误解了用法?
更新--这是我模糊的想法。我的配置文件有问题。如果您的配置文件的其余部分是正常的,那么上面的内容应该可以工作。
发布于 2014-02-27 01:45:06
这对我来说是模糊的想法--我的配置文件的其余部分都有问题。
基于Ben的例子,我想出了一个更容易测试的输入:
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。
发布于 2014-02-26 10:27:23
下面是我的配置。类型字段不存在,因此,负表达式返回true。
input {
stdin {
}
}
filter {
if [type] !~ /(.+)/ {
mutate { add_tag => ["aa"] }
}
}
output {
stdout {debug => true}
}
regexp /(.+)/表示它接受一切,包括空白。因此,当"type“字段存在时,即使字段值为空,它也会满足regexp。因此,在您的示例中,如果存在remote_ip字段,则“负表达式”将始终返回false。
https://stackoverflow.com/questions/22032818
复制相似问题