1

I'm using Grafana and Prometheus to create some graphs, but this I believe is fundamentally a regexp (RE2?) question. I am dynamically creating a list of elements and populating a Variable (Query of label_values(source) where "source" contains the list of possible results). The query returns a list like this:

udr_foo
udr_owls
dns-baz1
dns-baz399
rpz_c_1.donotuse
rpz_c_1.memosis
rpz_c_1.argylesocks
rpz_c_1.argylesocks3
rpz_c_1.argylesocks_http2

I cannot modify the data in the database; I must trim it down with regexp. Currently, I have this RE2 regexp that I bodged together that I apply to the list to do some exclusions:

/^(?!dns|udr|rpzlog_c_1.donotuse).*/

This gives me as a result that is partially useful, because it excludes the results I don't want:

rpz_c_1.memosis
rpz_c_1.argylesocks
rpz_c_1.argylesocks3
rpz_c_1.argylesocks_http2

Question: How would I modify that regular expression so it gives me a more concise result set by also stripping the leading "rpz_c_1." string component? As this is embedded in the Grafana tool, I cannot "pipe" multiple regexp instantiations together with a shell - I only get one regexp opportunity to modify the results. This is the set of results that I would like to have returned:

memosis
argylesocks
argylesocks3
argylesocks_http2

My regexp probably is awful. A more concise way of looking at this might be:

  • return all results that contain "rpz_c_1." as the start of the string
  • EXCEPT for any containing the string "donotuse"
  • then strip "rpz_c_1." from the beginning of each string
John Todd
  • 53
  • 1
  • 3
  • 7
  • 1
    Not sure if it is supported, but you might use a capturing group `^rpz_c_1\.(?!donotuse)(\S+)` https://regex101.com/r/seoQBf/1 – The fourth bird May 15 '20 at 16:45
  • The fourth bird: Sadly, that does not seem to be supported in Grafana. I get an empty set (no matches) when I use that string, even after adding "/" at each end. – John Todd May 15 '20 at 19:53
  • 1
    I spoke too quickly (or rather, I cut and pasted too quickly.) That does indeed seem to give the intended results! – John Todd May 15 '20 at 20:03

1 Answers1

5

You could simplify the negative lookahead by starting the match with rpz_c_1. After matching the dot, assert what is on the right is not donotuse

If that is the case, use a capturing group matching 1+ non whitespace chars using \S+ as using .* would match any char except a newline 0+ times.

^rpz_c_1\.(?!donotuse)(\S+)

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70