How to 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 operations such as filtering rows, dropping and keeping columns, state detection, etc. This guide shows how to use regular expressions in your Flux scripts.

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: "telegraf/autogen")
  |> 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: "telegraf/autogen")
  |> 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: "telegraf/autogen")
  |> range(start: -15m)
  |> filter(fn: (r) => r._measurement == "mem")
  |> drop(fn: (col) => col !~ /_.*/)
Syntax documentation

regexp Syntax GoDoc
RE2 Syntax Overview

Go regex testers

Regex Tester - Golang
Regex101

This documentation is open source. See a typo? Please, open an issue.


Need help getting up and running? Get Support