ruby - 如何匹配看起来像列的非结构化字符串中的 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
但我想有更好的方法来做到这一点
解决方案
我自己没有对此进行测试,但我很确定kv
过滤器可能是这里的方法。它接受field_split_pattern
哪个可以是多个空格的正则表达式,value_split_pattern
哪个可以是" :"
.
kv
过滤器的文档。
推荐阅读
- java - 即使 Junit 是绿色的,Wiremock 也显示零代码覆盖率
- powershell - 在Powershell中重新排列IF语句时的输出切换
- mongodb - 从 MongoDB 集合创建密钥列表 - Mongoosh / MongoDB 脚本
- odoo-14 - 在odoo14中删除自动完成
- .htaccess - .htaccess 的尾部斜杠问题
- go - 如何使用 go-git 包获取特定哈希的 git 标签以获取类似于 git describe --tags --always 的输出
- javascript - Formik React 电子邮件域验证
- regex - sed:匹配ANSI颜色的特殊字符
- python-3.x - 训练cnn时出现ResourceExhaustedError
- spring-webflux - 限制通过 spring webflux 发送的数据