Grafana 正则表达式忽略星号作为标签中的第一个字符

Grafana regex to ignore the asterisk as the first character in labels

我有 Grafana 5.2 仪表板从 Prometheus 获取数据。

我在仪表板中有一些标签似乎采用 *.<domain> 格式,例如*.google.com e.t.c 但是,如果没有一些智能正则表达式来忽略前两个字符,这不会与 Grafana 一起玩。

我有以下正则表达式 (?<=^\*\.|^)[-a-zA-Z0-9._ ]+,它似乎在 Grafana 中不起作用,但在 regex101 中有效。它应该导致标签为 google.com 即没有前两个字符 *..

谁能告诉我这是什么原因造成的?

根据 Grafana documentation,您可以将正则表达式的一部分捕获到 return 该子字符串:

Filter and modify the options using a regex capture group to return part of the text: Regex:

/.*(01|02)/

Result:

01
02

因此,您可以使用

^(?:\*\.)?([-a-zA-Z0-9._ ]+)
          ^                ^

参见regex demo

这里,

  • ^ - 字符串的开头
  • (?:\*\.)? - 可选(由于 ? 量词匹配 1 或 0 个序列)non-capturing group 匹配 *. 子串(1 或 0 次)
  • ([-a-zA-Z0-9._ ]+) - 匹配 1+ 个 ASCII 字母、数字、-._ 和 space 的 capturing group 和将其匹配值放入第 1 组,并 return 作为匹配结果将其放入 Grafana。