Documentation

Work with regular expression types

A regular expression type represents a regular expression pattern.

Type name: regexp

Regular expression syntax

Flux uses the Go regexp implementation and syntax. This syntax is similar to regular expressions in Perl, Python, and other languages. Regular expression literals are enclosed in forward slash characters (/).

/^[a-z0-9]+$/
  • Copy
  • Fill window

Use regular expression flags

Flux supports the following regular expression flags:

FlagDescription
icase-insensitive
mmulti-line mode: ^ and $ match begin/end line in addition to begin/end text
slet . match \n
Uungreedy: swap meaning of x* and x*?, x+ and x+?, etc

Include regular expression flags at the beginning of your regular expression pattern enclosed in parentheses (()) and preceded by a question mark (?).

/(?iU)foo*/
  • Copy
  • Fill window

Use regular expressions in predicate expressions

To use regular expressions in predicate expressions, use the =~ and !~ comparison operators. The left operand must be a string. The right operand must be a regular expression.

"abc" =~ /\w/
// Returns true

"z09se89" =~ /^[a-z0-9]{7}$/
// Returns true

"foo" !~ /^f/
// Returns false

"FOO" =~ /(?i)foo/
// Returns true
  • Copy
  • Fill window

Convert a string to a regular expression

  1. Import the regexp package.
  2. Use regexp.compile() to compile a string into a regular expression type.
import "regexp"

regexp.compile(v: "^- [a-z0-9]{7}")
// Returns ^- [a-z0-9]{7} (regexp type)
  • Copy
  • Fill window

Examples

Replace all substrings that match a regular expression

  1. Import the regexp package.

  2. Use regexp.replaceAllString() and provide the following parameters:

    • r: regular expression
    • v: string to search
    • t: replacement for matches to r.
import "regexp"

regexp.replaceAllString(r: /a(x*)b/, v: "-ab-axxb-", t: "T")
// Returns "-T-T-"
  • Copy
  • Fill window

Return the first regular expression match in a string

  1. Import the regexp package.

  2. Use regexp.findString() to return the first regular expression match in a string. Provide the following parameters:

    • r: regular expression
    • v: string to search
import "regexp"

regexp.findString(r: /foo.?/, v: "seafood fool")
// Returns "food"
  • Copy
  • Fill window

Escape regular expression metacharacters in a string

If a string contains regular expression metacharacters that should be evaluated as literal characters, escape the metacharacters before converting the string to a regular expression:

  1. Import the regexp package.
  2. Use regexp.quoteMeta() and provide the string to escape regular expression metacharacters in:
import "regexp"

regexp.quoteMeta(v: ".+*?()|[]{}^$")
// Returns "\.\+\*\?\(\)\|\[\]\{\}\^\$"
  • Copy
  • Fill window

Was this page helpful?

Thank you for your feedback!


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.

Read more

InfluxDB 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out: