How to write a regular matching expression

Hello guys!If server_name = www.test.com I want to match www.test.com www.test.com:9899 www.test.com:8888 www.test.com:8686 How to write the regular expression|> filter(fn: (r) => r[“server_name”] =~ /${server_name:regex}/)That doesn’t sound right.

@tengdagg Is ${server_name:regex} loading a Grafana variable?

@scott Yes, it’s a variable of grafana, the value of the variable is www.test.com and I want to match the server_name of www.test.com www.test.com:9899 www.test.com:8888 www.test.com:8686 which are inside influxdbv2. How do I write the regular expression, the database field is also server_name.

All Grafana variables are loaded and parsed as strings, so you first have to convert the string to a regular expression type. You can do this with the regexp.compile() function. Flux also uses the Go regular expression syntax, so your query would look something like this:

import "regexp"

server_regex = regexp.compile(v: "${server_name:regex}(:\d{4})?")

from(bucket: "example-bucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r[“server_name”] =~ server_regex)

The server_regex variable returns a regular expression type that should match whatever server value is selected as well as any 4 digit port that follows it (that’s what (:\d{4})? does).

@scott I’m so sorry I still need your help.
I changed my query statement in grafan the way you did, but Grafan is reporting this in error

My query statement

import "regexp"
server_regex = regexp.compile(v: "${server_name:regex}(:\d{4})?")
from(bucket: "nginx_prod")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "nginx_log")
  |> filter(fn: (r) => r["server_name"] =~ server_regex) 
  |> filter(fn: (r) => r["_field"] == "auth")
  |> group()
  |> count()

ERROR:
Status: 500. Message: invalid: compilation failed: error @3:16-11:13: expected RPAREN, got EOF error @3:34-3:39: got unexpected token in string expression @3:38-3:39: ILLEGAL error @3:53-3:57: invalid expression @3:52-3:53: \ error @3:58-3:64: missing property key error @3:60-3:61: invalid expression @3:59-3:60: \ error @3:62-3:63: unexpected token for property key: INT (4) error @3:66-5:15: invalid expression @3:65-3:66: ? error @9:44-11:13: got unexpected token in string expression @11:13-11:13: EOF

@tengdagg Can you inspect the actual query that Grafana sent to the InfluxDB query API? I’m wondering if something about how the variable value is being inserted is breaking the syntax of the query.