Flux: regular expression capture groups

I’d like to match a string to a regular expression and use captured substring elsewhere, for example, /foo([a-z]*)bar/. AFAIU, there is currently no way to use capture groups in current regexp package. Is my understanding correct? If yes, where do I submit feature requests? :slight_smile:

Hello @abbradar,
You can just filter with regex. You don’t need to use the regexp package:
https://docs.influxdata.com/influxdb/cloud/query-data/flux/regular-expressions/

@abbradar you might also find the following useful:
https://docs.influxdata.com/flux/v0.x/stdlib/strings/
Specifically:
https://docs.influxdata.com/flux/v0.x/stdlib/strings/containsstr/

Can you please provide a full example of what you’re trying to achieve so I can help you better?

I’d like to extract a particular substring from a name of systemd service. Currently I achieve this in a somewhat ugly way by doing two replacements:

  |> filter(fn: (r) => r["name"] =~ /^my-service-([^.]*)\.service$/)
  |> map(fn: (r) => {
    region_ = regexp.replaceAllString(r: /^my-service-/, t: "", v: r["name"])
    region = regexp.replaceAllString(r: /\.service$/, t: "", v: region_)
    return { r with region: region }
  })

It would be nice to have regular expressions with capture groups instead to achieve this. Say, regexp.newReplaceAllString(r: /^my-service-([^.]*)\.service$/, t: "\1", v: r["name"]), that should just yield needed string. It can also be used for more complex operations, for example regexp.newReplaceAllString(r: /^my-service-([^-]*)-([^.]*)\.service$/, t: "\2 \1", v: r["name"]), when used on string “my-service-foo-bar.service”, should yield “bar foo”.