Provides regex patterns, syntax, flags, and usage examples for Terraform regex functions: regex, regexall, and replace. Use this skill when asked about regular expressions, pattern matching, or string replacement in Terraform code.
This skill provides a curated set of regular expression (regex) patterns and usage examples for use with Terraform's built-in regex functions:
regex(pattern, string)regexall(pattern, string)replace(string, substring/pattern, replacement)It also covers syntax, sequences, and flags as documented in the Terraform regex function documentation.
Terraform uses RE2 syntax. Flags are set inline in the pattern, e.g. for case-insensitive.
(?i)| Sequence | Description |
|---|---|
. | any character except newline |
^ | start of string |
$ | end of string |
* | zero or more |
+ | one or more |
? | zero or one |
[...] | character class |
[xyz] | any character listed between the brackets (x, y, z) |
[^xyz] | any character except those listed between the brackets |
(...) | capture group |
\d | ASCII digits [0-9] |
\D | anything except ASCII digits |
\w | word character [0-9A-Za-z_] |
\W | anything except the characters matched by \w |
\s | whitespace |
[[:alnum:]] | same as [0-9A-Za-z] |
[[:alpha:]] | same as [A-Za-z] |
[[:ascii:]] | any ASCII character |
[[:blank:]] | ASCII tab or space |
[[:cntrl:]] | ASCII/Unicode control characters |
[[:digit:]] | same as [0-9] |
[[:graph:]] | all "graphical" (printable) ASCII characters |
[[:lower:]] | same as [a-z] |
[[:print:]] | same as [[:graph:]] |
[[:punct:]] | same as [!-/:-@[-{-~]` |
[[:space:]] | same as [\t\n\v\f\r ] |
[[:upper:]] | same as [A-Z] |
[[:word:]] | same as \w |
[[:xdigit:]] | same as [0-9A-Fa-f] |
Some of the matching behaviors described above can be modified by setting matching flags, activated using either the (?flags) operator (to activate within the current sub-pattern) or the (?flags:x) operator (to match x with the modified flags). Each flag is a single letter, and multiple flags can be set at once by listing multiple letters in the flags position. The available flags are listed in the table below:
| Flag | Meaning |
| i | Case insensitive: a literal letter in the pattern matches both lowercase and uppercase versions of that letter |
| m | The ^ and $ operators also match the beginning and end of lines within the string, marked by newline characters; behavior of \A and \z is unchanged |
| s | The . operator also matches newline |
| U | The meaning of presence or absense ? after a repetition operator is inverted. For example, x* is interpreted like x*? and vice-versa. |
regex("^[\w._%+-]+@[\w.-]+\\.[a-zA-Z]{2,}$", var.email)
regexall("\\d+", var.input)
replace(var.input, "\\s+", "-")
regex("^arn:aws:[a-z0-9-]+:[a-z0-9-]*:[0-9]*:.*$", var.arn)
regex("(?i)^prod$", var.env)
regex("[a-z]+", "53453453.345345aaabbbccc23454")
# returns: aaabbbccc
regexall("\\d+", "2019-02-01")
# returns: ["2019", "02", "01"]
regex("^(?:(?P<scheme>[^:/?#]+):)?(?://(?P<authority>[^/?#]*))?", "https://terraform.io/docs/")
# returns: { "authority" = "terraform.io", "scheme" = "https" }
regex("[a-z]+", "53453453.34534523454")
# error: pattern did not match any part of the given string
| Pattern | Description | Example |
|---|---|---|
^prod$ | Exact match for "prod" | regex("^prod$", var.env) |
(?i)^dev$ | Case-insensitive "dev" | regex("(?i)^dev$", var.env) |
^\d{4}-\d{2}-\d{2}$ | Date YYYY-MM-DD | regex("^\d{4}-\d{2}-\d{2}$", var.date) |
^[a-zA-Z0-9_-]+$ | Alphanumeric, dash, underscore | regex("^[a-zA-Z0-9_-]+$", var.name) |
^arn:aws:[a-z0-9-]+:[a-z0-9-]*:[0-9]*:.*$ | AWS ARN | regex("^arn:aws:[a-z0-9-]+:[a-z0-9-]*:[0-9]*:.*$", var.arn) |
| `(?i)error | fail | critical` |
^\s*$ | Empty or whitespace | regex("^\s*$", var.input) |
regex, regexall, or replace functions in Terraform.regexall for all matches.