You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Fluentbit正则表达式提取XML属性生成键值对?

为Fluentbit Parser编写正则提取XML属性

完整Parser配置

直接用以下Fluentbit Parser配置即可实现需求,自动处理"转义符并提取目标属性:

[PARSER]
    Name        testsuite_xml
    Format      regex
    Regex       ^<testsuite name=&quot;(.*?)&quot; tests=&quot;(\d+)&quot; errors=&quot;(\d+)&quot; failures=&quot;(\d+)&quot; skipped=&quot;(\d+)&quot; time=&quot;[^&]+&quot; timestamp=&quot;(.*?)&quot;>$
    Keys        name, tests, errors, failures, skipped, timestamp
    Types       tests:integer, errors:integer, failures:integer, skipped:integer

正则逻辑拆解

  • ^<testsuite:匹配字符串开头的<testsuite标签前缀
  • name=&quot;(.*?)&quot;:精准匹配name属性的转义引号结构,.*?是非贪婪捕获,只取引号之间的实际值(自动跳过&quot;)
  • tests=&quot;(\d+)&quot;:匹配tests等数字型属性,\d+只捕获数字内容,后续通过Types字段转为整数类型
  • time=&quot;[^&]+&quot;:匹配不需要的time属性但不捕获,[^&]+确保只匹配到该属性的结束转义引号
  • timestamp=&quot;(.*?)&quot;:捕获timestamp的完整时间字符串
  • >$:匹配标签结尾的>符号

效果验证

当输入目标XML字符串时,Fluentbit会输出如下键值对(与预期完全一致):

name: Activity moved
tests: 1
errors: 0
failures: 0
skipped: 0
timestamp: 2022-09-05T16:22:53.184000

注意事项

如果XML标签的属性顺序可能变动,上述固定顺序的正则会失效。这种情况下,建议搭配Fluentbit的lua过滤器实现更灵活的XML属性提取,或者使用xml格式解析器(需确保Fluentbit启用了XML插件)。

内容的提问来源于stack exchange,提问作者Zain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:05:27