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 being 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!


Select your region

Linux Package Signing Key Rotation

All signed InfluxData Linux packages have been resigned with an updated key. If using Linux, you may need to update your package configuration to continue to download and verify InfluxData software packages.

For more information, see the Linux Package Signing Key Rotation blog post.

InfluxDB Cloud backed by InfluxDB IOx

All InfluxDB Cloud organizations created on or after January 31, 2023 are backed by the new InfluxDB IOx storage engine. Check the right column of your InfluxDB Cloud organization homepage to see which InfluxDB storage engine you’re using.

If powered by IOx, this is the correct documentation.

If powered by TSM, see the TSM-based InfluxDB Cloud documentation.

InfluxDB Cloud backed by InfluxDB TSM

All InfluxDB Cloud organizations created on or after January 31, 2023 are backed by the new InfluxDB IOx storage engine which enables nearly unlimited series cardinality and SQL query support. Check the right column of your InfluxDB Cloud organization homepage to see which InfluxDB storage engine you’re using.

If powered by TSM, this is the correct documentation.

If powered by IOx, see the IOx-based InfluxDB Cloud documentation.

State of the InfluxDB Cloud (IOx) documentation

The new documentation for InfluxDB Cloud backed by InfluxDB IOx is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.