首页 > 解决方案 > 如何匹配看起来像列的非结构化字符串中的 2 个键值对

问题描述

我有一个文档,其中包含以下几行:

  Example requested: 24:00:00            Example Used: 01:14:11        

我想要的是:

{
"example_requested": "24:00:00",
"example_used": 01:14:11
}

我试过的(这是ruby块):

if x =~ /(Example requested)/
        example_requested = x.scan(/^(?:.*?: ){1}(.*)$/)[0]
        example_requested = (memory_requested * "").gsub(/\s+/, "")

我明白了

"example_requested" : "24:00:00ExampleUsed:01:14:11"

在https://rubular.com/r/WOmAqx4YkOnDER中尝试了一些组合,但是当我将它放入ruby code块中时,这段代码实际上并没有像这里那样工作Logstash。只有在这里真正起作用的是用 . 跳过空格gsub。任何帮助表示赞赏

更新

if x =~ /(Example Requested)/
        example_used = x.scan(/^(?:.*?: ){2}(.*)$/)[0]
        example_used = (example_used * "").gsub(/\s+/, "")

实际结果是:

"example_used": 01:14:11

但我想有更好的方法来做到这一点

标签: rubylogstash

解决方案


我自己没有对此进行测试,但我很确定kv过滤器可能是这里的方法。它接受field_split_pattern哪个可以是多个空格的正则表达式,value_split_pattern哪个可以是" :".

kv过滤器的文档。


推荐阅读