---
title: Use regular expressions in Flux
description: This guide walks through using regular expressions in evaluation logic in Flux functions.
url: https://docs.influxdata.com/influxdb/v2/query-data/flux/regular-expressions/
estimated_tokens: 2105
product: InfluxDB OSS v2
version: v2
---

# Use regular expressions in Flux

This page documents an earlier version of InfluxDB OSS. [InfluxDB 3 Core](/influxdb3/core/) is the latest stable version.

#### API token hashing is enabled by default in InfluxDB OSS 2.9.0

Stronger token security: tokens are stored as hashes on disk, so a copy of the database file doesn’t expose usable tokens. Existing tokens are hashed on first startup and the original strings can’t be recovered afterward — **capture any plaintext tokens you still need before you upgrade**.

For more information, see [Token hashing](/influxdb/v2/admin/tokens/#token-hashing).

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](/flux/v0/get-started/) for a conceptual overview of Flux and parts of a Flux query.
-   [Execute queries](/influxdb/v2/query-data/execute-queries/) to discover a variety of ways to run your queries.

## Go regular expression syntax

Flux uses Go’s [regexp package](https://golang.org/pkg/regexp/) for regular expression search. The links [below](#helpful-links) 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

```js
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`.

```js
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.

```js
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 `_`.

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

## Helpful links

##### Syntax documentation

[regexp Syntax GoDoc](https://godoc.org/regexp/syntax)  
[RE2 Syntax Overview](https://github.com/google/re2/wiki/Syntax)

##### Go regex testers

[Regex Tester - Golang](https://regex-golang.appspot.com/assets/html/index.html)  
[Regex101](https://regex101.com/)

#### Related

-   [Query fields and tags](/influxdb/v2/query-data/flux/query-fields/)
-   [regexp package](/flux/v0/stdlib/regexp/)

[regex](/influxdb/v2/tags/regex/)
