github.com/hugorut/terraform@v1.1.3/website/docs/language/functions/regex.mdx (about)

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