github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration/functions/regex.html.md (about)

     1  ---
     2  layout: "functions"
     3  page_title: "regex - Functions - Configuration Language"
     4  sidebar_current: "docs-funcs-string-regex"
     5  description: |-
     6    The regex function applies a regular expression to a string and returns the
     7    matching substrings.
     8  ---
     9  
    10  # `regex` Function
    11  
    12  -> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
    13  earlier, see
    14  [0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
    15  
    16  `regex` applies a
    17  [regular expression](https://en.wikipedia.org/wiki/Regular_expression)
    18  to a string and returns the matching substrings.
    19  
    20  ```hcl
    21  regex(pattern, string)
    22  ```
    23  
    24  The return type of `regex` depends on the capture groups, if any, in the
    25  pattern:
    26  
    27  - If the pattern has no capture groups at all, the result is a single string
    28    covering the substring matched by the pattern as a whole.
    29  - If the pattern has one or more _unnamed_ capture groups, the result is a
    30    list of the captured substrings in the same order as the definition of
    31    the capture groups.
    32  - If the pattern has one or more _named_ capture groups, the result is a
    33    map of the captured substrings, using the capture group names as map keys.
    34  
    35  It's not valid to mix both named and unnamed capture groups in the same pattern.
    36  
    37  If the given pattern does not match at all, the `regex` raises an error. To
    38  _test_ whether a given pattern matches a string, use
    39  [`regexall`](./regexall.html) and test that the result has length greater than
    40  zero.
    41  
    42  The pattern is a string containing a mixture of literal characters and special
    43  matching operators as described in the following table. Note that when giving a
    44  regular expression pattern as a literal quoted string in the Terraform
    45  language, the quoted string itself already uses backslash `\` as an escape
    46  character for the string, so any backslashes intended to be recognized as part
    47  of the pattern must be escaped as `\\`.
    48  
    49  | Sequence       | Matches                                                                          |
    50  | -------------- | -------------------------------------------------------------------------------- |
    51  | `.`            | Any character except newline                                                     |
    52  | `[xyz]`        | Any character listed between the brackets (`x`, `y`, and `z` in this example)    |
    53  | `[a-z]`        | Any character between `a` and `z`, inclusive                                     |
    54  | `[^xyz]`       | The opposite of `[xyz]`                                                          |
    55  | `\d`           | ASCII digits (0 through 9, inclusive)                                            |
    56  | `\D`           | Anything except ASCII digits                                                     |
    57  | `\s`           | ASCII spaces (space, tab, newline, carriage return, form feed)                   |
    58  | `\S`           | Anything except ASCII spaces                                                     |
    59  | `\w`           | The same as `[0-9A-Za-z_]`                                                       |
    60  | `\W`           | Anything except the characters matched by `\w`                                   |
    61  | `[[:alnum:]]`  | The same as `[0-9A-Za-z]`                                                        |
    62  | `[[:alpha:]]`  | The same as `[A-Za-z]`                                                           |
    63  | `[[:ascii:]]`  | Any ASCII character                                                              |
    64  | `[[:blank:]]`  | ASCII tab or space                                                               |
    65  | `[[:cntrl:]]`  | ASCII/Unicode control characters                                                 |
    66  | `[[:digit:]]`  | The same as `[0-9]`                                                              |
    67  | `[[:graph:]]`  | All "graphical" (printable) ASCII characters                                     |
    68  | `[[:lower:]]`  | The same as `[a-z]`                                                              |
    69  | `[[:print:]]`  | The same as `[[:graph:]]`                                                        |
    70  | `[[:punct:]]`  | The same as `` [!-/:-@[-`{-~] ``                                                 |
    71  | `[[:space:]]`  | The same as `[\t\n\v\f\r ]`                                                      |
    72  | `[[:upper:]]`  | The same as `[A-Z]`                                                              |
    73  | `[[:word:]]`   | The same as `\w`                                                                 |
    74  | `[[:xdigit:]]` | The same as `[0-9A-Fa-f]`                                                        |
    75  | `\pN`          | Unicode character class by using single-letter class names ("N" in this example) |
    76  | `\p{Greek}`    | Unicode character class by unicode name ("Greek" in this example)                |
    77  | `\PN`          | The opposite of `\pN`                                                            |
    78  | `\P{Greek}`    | The opposite of `\p{Greek}`                                                      |
    79  | `xy`           | `x` followed immediately by `y`                                                  |
    80  | `x|y`          | either `x` or `y`, preferring `x`                                                |
    81  | `x*`           | zero or more `x`, preferring more                                                |
    82  | `x*?`          | zero or more `x`, preferring fewer                                               |
    83  | `x+`           | one or more `x`, preferring more                                                 |
    84  | `x+?`          | one or more `x`, preferring fewer                                                |
    85  | `x?`           | zero or one `x`, preferring one                                                  |
    86  | `x??`          | zero or one `x`, preferring zero                                                 |
    87  | `x{n,m}`       | between `n` and `m` repetitions of `x`, preferring more                          |
    88  | `x{n,m}?`      | between `n` and `m` repetitions of `x`, preferring fewer                         |
    89  | `x{n,}`        | at least `n` repetitions of `x`, preferring more                                 |
    90  | `x{n,}?`       | at least `n` repetitions of `x`, preferring fewer                                |
    91  | `x{n}`         | exactly `n` repetitions of `x`                                                   |
    92  | `(x)`          | unnamed capture group for sub-pattern `x`                                        |
    93  | `(?P<name>x)`  | named capture group, named `name`, for sub-pattern `x`                           |
    94  | `(?:x)`        | non-capturing sub-pattern `x`                                                    |
    95  | `\*`           | Literal `*` for any punctuation character `*`                                    |
    96  | `\Q...\E`      | Literal `...` for any text `...` as long as it does not include literally `\E`   |
    97  
    98  In addition to the above matching operators that consume the characters they
    99  match, there are some additional operators that _only_ match, but consume
   100  no characters. These are "zero-width" matching operators:
   101  
   102  | Sequence | Matches                                                                                          |
   103  | -------- | ------------------------------------------------------------------------------------------------ |
   104  | `^`      | At the beginning of the given string                                                             |
   105  | `$`      | At the end of the given string                                                                   |
   106  | `\A`     | At the beginning of the given string                                                             |
   107  | `\z`     | At the end of the given string                                                                   |
   108  | `\b`     | At an ASCII word boundary (transition between `\w` and either `\W`, `\A` or `\z`, or vice-versa) |
   109  | `\B`     | Not at an ASCII word boundary                                                                    |
   110  
   111  Terraform uses the
   112  [RE2](https://github.com/google/re2/wiki/Syntax) regular expression language.
   113  This engine does not support all of the features found in some other regular
   114  expression engines; in particular, it does not support backreferences.
   115  
   116  ## Matching Flags
   117  
   118  Some of the matching behaviors described above can be modified by setting
   119  matching flags, activated using either the `(?flags)` operator (to activate
   120  within the current sub-pattern) or the `(?flags:x)` operator (to match `x` with
   121  the modified flags). Each flag is a single letter, and multiple flags can be
   122  set at once by listing multiple letters in the `flags` position.
   123  The available flags are listed in the table below:
   124  
   125  | Flag | Meaning                                                                                                                                                     |
   126  | ---- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
   127  | `i`  | Case insensitive: a literal letter in the pattern matches both lowercase and uppercase versions of that letter                                              |
   128  | `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 |
   129  | `s`  | The `.` operator also matches newline                                                                                                                       |
   130  | `U`  | The meaning of presence or absense `?` after a repetition operator is inverted. For example, `x*` is interpreted like `x*?` and vice-versa.                 |
   131  
   132  ## Examples
   133  
   134  ```
   135  > regex("[a-z]+", "53453453.345345aaabbbccc23454")
   136  aaabbbccc
   137  
   138  > regex("(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)", "2019-02-01")
   139  [
   140    "2019",
   141    "02",
   142    "01",
   143  ]
   144  
   145  > regex("^(?:(?P<scheme>[^:/?#]+):)?(?://(?P<authority>[^/?#]*))?", "https://terraform.io/docs/")
   146  {
   147    "authority" = "terraform.io"
   148    "scheme" = "https"
   149  }
   150  
   151  > regex("[a-z]+", "53453453.34534523454")
   152  
   153  Error: Error in function call
   154  
   155  Call to function "regex" failed: pattern did not match any part of the given
   156  string.
   157  ```
   158  
   159  ## Related Functions
   160  
   161  - [`regexall`](./regexall.html) searches for potentially multiple matches of a given pattern in a string.
   162  - [`replace`](./replace.html) replaces a substring of a string with another string, optionally matching using the same regular expression syntax as `regex`.
   163  
   164  If Terraform already has a more specialized function to parse the syntax you
   165  are trying to match, prefer to use that function instead. Regular expressions
   166  can be hard to read and can obscure your intent, making a configuration harder
   167  to read and understand.