github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/functions/regex.html.md (about)

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