regex - 提取最后一个词/文本多个匹配的 logstash

标签 regex logstash

我有一个用于摄取整个文件的 Logstash 管道,使用了多行代码。我想获取所有匹配事件并仅提取最后一个单词或文本。我无法使用任何正则表达式。

文件内容

some line extract this 875846 85746,857
some other line
some other line with more text
let's extract this 887362        24153,44737
some other final line

必需匹配

查找所有包含“extract this”的行并检索最后一个单词/文本

预期输出

{
    "patternmatch1" => [
        [0] [
            [0] "85746,857"
        ],
        [1] [
            [0] "24153,44737"
        ]
    ],
       "@timestamp" => 2020-01-14T11:15:34.304Z
}

Logstash 管道

input {
    file{
        path => "C:/file.txt"
        start_position => "beginning"
        sincedb_path => NUL
        codec => multiline { 
            pattern => "^nomatching"
            negate => true
            what => previous
            auto_flush_interval => 1
            multiline_tag => ""
        }
    }
}
filter {
  ruby { code => 'event.set("patternmatch1",event.get("message").scan(/extract this([^\r]*)/))' }
}
output {   
  stdout { codec => rubydebug } 
}

当前输出

{
    "patternmatch1" => [],
     "message" => "some line extract this 875846 85746,857\r\nsome other line\r\nsome other line with more text\r\nlet's extract this 887362        24153,44737\r\nsome other final line\r\n\r",
   "@timestamp" => 2020-01-14T11:44:50.140Z
}

最佳答案

您可以使用以下正则表达式:

/extract this.*?(\d[\d,]*)\r?$/

会匹配

  • extract this - literally
  • .*? - 除换行符以外的任何 0+ 个字符尽可能少
  • (\d[\d,]*) - 第 1 组(scan 返回的内容):一个数字后跟 0+ 个数字或逗号
  • \r? - 一个可选的 CR(回车)
  • $ - 行尾。

请注意,由于文件中的行结尾是 CRLF,因此您不能仅使用 $ 来匹配行尾位置,您应该使用 \r?$.

关于regex - 提取最后一个词/文本多个匹配的 logstash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59732908/

相关文章:

javascript - 修复 Javascript 无效的正则表达式 : Invalid Group

elasticsearch - Logstash Elastic Cloud 401未经授权的错误

elasticsearch - 如何在 Kibana、Logstash 和 ElasticSearch 中使用 IP 地址在世界地图中显示计数?

python - re.finditer() 实例是否无法匹配原始模式?

php - 正则表达式是查找一行 HTML 的正确工具吗?

python - 如何使用 elasticsearch python 中的特定字段名称从多个索引中删除文档?

java - 错误 : mongodb. jdbc.MongoDriver 未加载。您确定已在 :jdbc_driver_library? 中包含正确的 jdbc 驱动程序吗

linux - 日志和ELK栈在不同的服务器上,如何显示日志?

php - 如何在此正则表达式中允许空格?

javascript - 正则表达式匹配的内容