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

18,642

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.
Share:
18,642
nixgadget
Author by

nixgadget

Software engineer and a robotic enthusiast

Updated on June 13, 2022

Comments

  • nixgadget
    nixgadget almost 2 years

    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 *..

    Without regex

    With regex

    Can someone please let me know what causes this ?

    • Wiktor Stribiżew
      Wiktor Stribiżew over 5 years
      Try using a capturing group - ^(?:\*\.)?([-a-zA-Z0-9._ ]+)
    • Addison
      Addison over 5 years
      I assume this is because Grafana doesn't support lookbehinds. You could try using \K to reset the match ^(?:\*\.)?\K[-a-zA-Z0-9._ ]+. regex101.
    • Wiktor Stribiżew
      Wiktor Stribiżew over 5 years
      Also, the placeholder in that field suggests regex delimiters, and if they are required, try /^(?:\*\.)?([-a-zA-Z0-9._ ]+)/. Or, if it is PCRE/Boost/Onigmo powered, use /^(?:\*\.)?\K[-a-zA-Z0-9._ ]+/
    • nixgadget
      nixgadget over 5 years
      @WiktorStribiżew your first suggestion seems to have worked. I guess Grafana look* type regex. Thanks for the tip. Also feel free to add it as an answer so i can mark it