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:
- Get started with Flux for a conceptual overview of Flux and parts of a Flux query.
- Execute queries to discover a variety of ways to run your queries.
Go 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
When the expression on the left DOES NOT MATCH the regular expression on the right, this evaluates to
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/
Use a regex to filter by tag value
The following example filters records by the
It only keeps records for which the
cpu is either
from(bucket: "example-bucket") |> range(start: -15m) |> filter(fn: (r) => r._measurement == "cpu" and r._field == "usage_user" 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 !~ /_.*/)
Go regex testers
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support or submit feedback, the following resources are available:
- Post in the InfluxData Community.
- Submit documentation issues to the InfluxDB 2.0 documentation repository.
- Submit InfluxDB issues to the InfluxDB repository.
For our InfluxDB Cloud customers, InfluxData Support is also available.