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

正则表达式前瞻捕获组匹配问题:Grafana变量字段提取

解决方案

需求回顾

从以下输入文本中提取指定格式的text和value:

elasticsearch_sync_queue_lowerlevelgiberish_ManualAllocationPolicy:pending
elasticsearch_sync_queue_SynchronizationProcessor:processing

期望输出:

text=ManualAllocationPolicy 
value=elasticsearch_sync_queue_lowerlevelgiberish_ManualAllocationPolicy
text=SynchronizationProcessor
value=elasticsearch_sync_queue_SynchronizationProcessor

修正后的正则表达式

^(?<value>.*_(?<text>[A-Z][^:]+)):.*

正则说明

  • ^:匹配行起始位置
  • (?<value>.*_(?<text>[A-Z][^:]+)):
    • (?<value>...):命名捕获组,捕获冒号前的完整字符串作为value
    • .*_:匹配最后一个下划线之前的所有内容
    • (?<text>[A-Z][^:]+):命名捕获组,匹配最后一个下划线后以大写字母开头、直到冒号前的内容作为text
  • :.*:匹配冒号及之后的所有内容,确保整行被处理,避免残留干扰内容

适配Grafana变量

将该正则应用到Grafana变量的查询处理中时,Grafana会自动识别命名捕获组text和value,生成符合需求的键值对。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 17:13:25