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

吃可爱长大的小学妹 提交于 2019-12-24 02:05:27

问题


I have Grafana 5.2 dashboards sourcing data from Prometheus.

I have some labels in a dashboard that seem to be in the format *.<domain> for e.g. *.google.com e.t.c however, this doesn't play with Grafana without some smart regex to ignore the first two characters.

I have the following regex (?<=^\*\.|^)[-a-zA-Z0-9._ ]+ which doesn't seem to work in Grafana but works in regex101. It should result in the label as google.com i.e. without the first two characters *..

Can someone please let me know what causes this ?


回答1:


According to Grafana documentation, you may capture the part of a regex to return that substring:

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

/.*(01|02)/

Result:

01
02

Hence, you may use

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

See the regex demo.

Here,

  • ^ - start of a string
  • (?:\*\.)? - an optional (due to ? quantifier that matches 1 or 0 sequences) non-capturing group that matches a *. substring (1 or 0 times)
  • ([-a-zA-Z0-9._ ]+) - a capturing group that matches 1+ ASCII letters, digits, -, ., _ and space and places its matched value into Group 1 and returns it in Grafana as a result of a match.


来源:https://stackoverflow.com/questions/52363955/grafana-regex-to-ignore-the-asterisk-as-the-first-character-in-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!