# Flux: regular expression capture groups

**URL:** <https://community.influxdata.com/t/flux-regular-expression-capture-groups/21905>\
**Category:** InfluxDB 2\
**Tags:** regex, flux\
**Created:** [September 30, 2021, 3:17pm UTC](https://community.influxdata.com/t/flux-regular-expression-capture-groups/21905 "2021-09-30T15:17:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abbradar](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@abbradar](https://community.influxdata.com/u/abbradar)\
**Post date:** [September 30, 2021, 3:17pm UTC](https://community.influxdata.com/t/flux-regular-expression-capture-groups/21905/1 "2021-09-30T15:17:44Z")

</div>

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](https://docs.influxdata.com/flux/v0.x/stdlib/regexp/) to use capture groups in current `regexp` package. Is my understanding correct? If yes, where do I submit feature requests? 🙂

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [September 30, 2021, 3:41pm UTC](https://community.influxdata.com/t/flux-regular-expression-capture-groups/21905/2 "2021-09-30T15:41:44Z")

</div>

Hello @abbradar,  
You can just filter with regex. You don’t need to use the regexp package:

> **[Use regular expressions in Flux | InfluxDB Cloud Documentation](https://docs.influxdata.com/influxdb/cloud/query-data/flux/regular-expressions/)**
>
> This guide walks through using regular expressions in evaluation logic in Flux functions.

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [September 30, 2021, 3:45pm UTC](https://community.influxdata.com/t/flux-regular-expression-capture-groups/21905/3 "2021-09-30T15:45:38Z")

</div>

@abbradar you might also find the following useful:

> **[strings package | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/strings/)**
>
> The strings package provides functions to operate on UTF-8 encoded strings.

Specifically:

> **[strings.containsStr() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/strings/containsstr/)**
>
> strings.containsStr() reports whether a string contains a specified substring.

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

---

<div class="post-metadata">

**Author:** ![abbradar](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@abbradar](https://community.influxdata.com/u/abbradar)\
**Post date:** [September 30, 2021, 4:01pm UTC](https://community.influxdata.com/t/flux-regular-expression-capture-groups/21905/4 "2021-09-30T16:01:58Z")

</div>

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:

```auto
  |> 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”.
