Search code examples
regexgrafana-variable

Regexp & Grafana: exclusions and string cut


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

Solution

  • 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