1

Hello I'd like to create a variable with label_value and then replace the label by some regexp. The value should be entire string, the text should be only part which starts with uppercase (e.g. ManualAllocationPolicy:pending. I guess the regular expression is quite good, but it's not displayed as I want. What am I missing? Grafana version is 9.4.7

variable

select

bilak
  • 4,526
  • 3
  • 35
  • 75

1 Answers1

1

This is more of the regex problem, then Grafana problem. Your application of alteration for regex is incorrect: in case of alteration only one of groups will be matched (text in your exact case).

To match same text multiple times you'll need to employ a trick: capturing groups inside of lookaheads.

Your regex will look like this: ^(?=.*?(?<text>[A-Z].*))(?<value>.+)

For metric name

elasticsearch_sync_queue_lowerlevelgiberishManualAllocationPolicy:pending

It will produce two groups:

  • text: ManualAllocationPolicy:pending
  • value: elasticsearch_sync_queue_lowerlevelgiberishManualAllocationPolicy:pending

Demo of this regex matching online here. Matched groups can be seen on the right panel.

markalex
  • 8,623
  • 2
  • 7
  • 32
  • wonderful...thanks. Would you be able to remove also the `:pending`? I've tried with `[^:]` but I've probably placed it at wrong position. – bilak Apr 28 '23 at 08:34
  • either [regex101](https://regex101.com/r/oF70rG/1) is giving me false result with substitution or I'm doing it wrong – bilak Apr 28 '23 at 08:56
  • @bilak, if you want to remove `:pending` only from text use [`^(?=.*?(?[A-Z][^:]*))(?.+)`](https://regex101.com/r/oF70rG/2). – markalex Apr 28 '23 at 09:32
  • I want to remove from both text and value. – bilak Apr 28 '23 at 09:33
  • @bilak, than regex from your link is correct. If may not see green background for group `text`, but it is still matched (look at the right panel.) – markalex Apr 28 '23 at 09:35
  • I've seen that in panel, but it doesn't work even in grafana. So there might be some issue. – bilak Apr 28 '23 at 09:42
  • @bilak, hm, looks like Grafana have problema with `:` in group `value`. Surround regex with `/`, like this `/^(?=.*?(?[A-Z][^:]*))(?[^:]+)/`, it should work (works for me with similar setup) – markalex Apr 28 '23 at 09:54
  • this returns just one result...idk what's wrong with grafana – bilak Apr 28 '23 at 10:07
  • 1
    so finally this works `^(?=.*?(?[A-Z][^:]*))(?.+)(:.*)` – bilak Apr 28 '23 at 10:09