Documentation

Use regular expressions in Flux

Regular expressions (regexes) are incredibly powerful when matching patterns in large collections of data. With Flux, regular expressions are primarily used for evaluation logic in predicate functions for things such as filtering rows, dropping and keeping columns, state detection, etc. This guide shows how to use regular expressions in your Flux scripts.

If you’re just getting started with Flux queries, check out the following:

Go regular expression syntax

Flux uses Go’s regexp package for regular expression search. The links below provide information about Go’s regular expression syntax.

Regular expression operators

Flux provides two comparison operators for use with regular expressions.

=~

When the expression on the left MATCHES the regular expression on the right, this evaluates to true.

!~

When the expression on the left DOES NOT MATCH the regular expression on the right, this evaluates to true.

Regular expressions in Flux

When using regex matching in your Flux scripts, enclose your regular expressions with /. The following is the basic regex comparison syntax:

Basic regex comparison syntax
expression =~ /regex/
expression !~ /regex/

Examples

Use a regex to filter by tag value

The following example filters records by the cpu tag. It only keeps records for which the cpu is either cpu0, cpu1, or cpu2.

from(bucket: "example-bucket")
    |> range(start: -15m)
    |> filter(fn: (r) => r._measurement == "cpu" and r.cpu =~ /cpu[0-2]$/)

Use a regex to filter by field key

The following example excludes records that do not have _percent in a field key.

from(bucket: "example-bucket")
    |> range(start: -15m)
    |> filter(fn: (r) => r._measurement == "mem" and r._field =~ /_percent/)

Drop columns matching a regex

The following example drops columns whose names do not begin with _.

from(bucket: "example-bucket")
    |> range(start: -15m)
    |> filter(fn: (r) => r._measurement == "mem")
    |> drop(fn: (column) => column !~ /^_.*/)
Syntax documentation

regexp Syntax GoDoc
RE2 Syntax Overview

Go regex testers

Regex Tester - Golang
Regex101


Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following: