github.com/wuhuizuo/gomplate@v3.5.0+incompatible/docs/content/functions/regexp.md (about)

     1  ---
     2  title: regexp functions
     3  menu:
     4    main:
     5      parent: functions
     6  ---
     7  
     8  These functions allow user you to search and modify text with regular expressions.
     9  
    10  The syntax of the regular expressions accepted is [Go's `regexp` syntax](https://golang.org/pkg/regexp/syntax/#hdr-Syntax),
    11  and is the same general syntax used by Perl, Python, and other languages.
    12  
    13  ## `regexp.Find`
    14  
    15  Returns a string holding the text of the leftmost match in `input`
    16  of the regular expression `expression`.
    17  
    18  This function provides the same behaviour as Go's
    19  [`regexp.FindString`](https://golang.org/pkg/regexp/#Regexp.FindString) function.
    20  
    21  ### Usage
    22  
    23  ```go
    24  regexp.Find expression input
    25  ```
    26  ```go
    27  input | regexp.Find expression
    28  ```
    29  
    30  ### Arguments
    31  
    32  | name | description |
    33  |------|-------------|
    34  | `expression` | _(required)_ The regular expression |
    35  | `input` | _(required)_ The input to search |
    36  
    37  ### Examples
    38  
    39  ```console
    40  $ gomplate -i '{{ regexp.Find "[a-z]{3}" "foobar"}}'
    41  foo
    42  ```
    43  ```console
    44  $ gomplate -i 'no {{ "will not match" | regexp.Find "[0-9]" }}numbers'
    45  no numbers
    46  ```
    47  
    48  ## `regexp.FindAll`
    49  
    50  Returns a list of all successive matches of the regular expression.
    51  
    52  This can be called with 2 or 3 arguments. When called with 2 arguments, the
    53  `n` argument (number of matches) will be set to `-1`, causing all matches
    54  to be returned.
    55  
    56  This function provides the same behaviour as Go's
    57  [`regexp.FindAllString`](https://golang.org/pkg/regexp/#Regexp.FindAllString) function.
    58  
    59  ### Usage
    60  
    61  ```go
    62  regexp.FindAll expression [false] input
    63  ```
    64  ```go
    65  input | regexp.FindAll expression [false]
    66  ```
    67  
    68  ### Arguments
    69  
    70  | name | description |
    71  |------|-------------|
    72  | `expression` | _(required)_ The regular expression |
    73  | `false` | _(optional)_ The number of matches to return |
    74  | `input` | _(required)_ The input to search |
    75  
    76  ### Examples
    77  
    78  ```console
    79  $ gomplate -i '{{ regexp.FindAll "[a-z]{3}" "foobar" | toJSON}}'
    80  ["foo", "bar"]
    81  ```
    82  ```console
    83  $ gomplate -i '{{ "foo bar baz qux" | regexp.FindAll "[a-z]{3}" 3 | toJSON}}'
    84  ["foo", "bar", "baz"]
    85  ```
    86  
    87  ## `regexp.Match`
    88  
    89  Returns `true` if a given regular expression matches a given input.
    90  
    91  This returns a boolean which can be used in an `if` condition, for example.
    92  
    93  ### Usage
    94  
    95  ```go
    96  regexp.Match expression input
    97  ```
    98  ```go
    99  input | regexp.Match expression
   100  ```
   101  
   102  ### Arguments
   103  
   104  | name | description |
   105  |------|-------------|
   106  | `expression` | _(required)_ The regular expression |
   107  | `input` | _(required)_ The input to test |
   108  
   109  ### Examples
   110  
   111  ```console
   112  $ gomplate -i '{{ if (.Env.USER | regexp.Match `^h`) }}username ({{.Env.USER}}) starts with h!{{end}}'
   113  username (hairyhenderson) starts with h!
   114  ```
   115  
   116  ## `regexp.Replace`
   117  
   118  Replaces matches of a regular expression with the replacement string.
   119  
   120  The replacement is substituted after expanding variables beginning with `$`.
   121  
   122  This function provides the same behaviour as Go's
   123  [`regexp.ReplaceAllString`](https://golang.org/pkg/regexp/#Regexp.ReplaceAllString) function.
   124  
   125  ### Usage
   126  
   127  ```go
   128  regexp.Replace expression replacement input
   129  ```
   130  ```go
   131  input | regexp.Replace expression replacement
   132  ```
   133  
   134  ### Arguments
   135  
   136  | name | description |
   137  |------|-------------|
   138  | `expression` | _(required)_ The regular expression string |
   139  | `replacement` | _(required)_ The replacement string |
   140  | `input` | _(required)_ The input string to operate on |
   141  
   142  ### Examples
   143  
   144  ```console
   145  $ gomplate -i '{{ regexp.Replace "(foo)bar" "$1" "foobar"}}'
   146  foo
   147  ```
   148  ```console
   149  $ gomplate -i '{{ regexp.Replace "(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)" "${last}, ${first}" "Alan Turing"}}'
   150  Turing, Alan
   151  ```
   152  
   153  ## `regexp.ReplaceLiteral`
   154  
   155  Replaces matches of a regular expression with the replacement string.
   156  
   157  The replacement is substituted directly, without expanding variables
   158  beginning with `$`.
   159  
   160  This function provides the same behaviour as Go's
   161  [`regexp.ReplaceAllLiteralString`](https://golang.org/pkg/regexp/#Regexp.ReplaceAllLiteralString) function.
   162  
   163  ### Usage
   164  
   165  ```go
   166  regexp.ReplaceLiteral expression replacement input
   167  ```
   168  ```go
   169  input | regexp.ReplaceLiteral expression replacement
   170  ```
   171  
   172  ### Arguments
   173  
   174  | name | description |
   175  |------|-------------|
   176  | `expression` | _(required)_ The regular expression string |
   177  | `replacement` | _(required)_ The replacement string |
   178  | `input` | _(required)_ The input string to operate on |
   179  
   180  ### Examples
   181  
   182  ```console
   183  $ gomplate -i '{{ regexp.ReplaceLiteral "(foo)bar" "$1" "foobar"}}'
   184  $1
   185  ```
   186  ```console
   187  $ gomplate -i '{{ `foo.bar,baz` | regexp.ReplaceLiteral `\W` `$` }}'
   188  foo$bar$baz
   189  ```
   190  
   191  ## `regexp.Split`
   192  
   193  Splits `input` into sub-strings, separated by the expression.
   194  
   195  This can be called with 2 or 3 arguments. When called with 2 arguments, the
   196  `n` argument (number of matches) will be set to `-1`, causing all sub-strings
   197  to be returned.
   198  
   199  This is equivalent to [`strings.SplitN`](../strings/#strings-splitn),
   200  except that regular expressions are supported.
   201  
   202  This function provides the same behaviour as Go's
   203  [`regexp.Split`](https://golang.org/pkg/regexp/#Regexp.Split) function.
   204  
   205  ### Usage
   206  
   207  ```go
   208  regexp.Split expression [false] input
   209  ```
   210  ```go
   211  input | regexp.Split expression [false]
   212  ```
   213  
   214  ### Arguments
   215  
   216  | name | description |
   217  |------|-------------|
   218  | `expression` | _(required)_ The regular expression |
   219  | `false` | _(optional)_ The number of matches to return |
   220  | `input` | _(required)_ The input to search |
   221  
   222  ### Examples
   223  
   224  ```console
   225  $ gomplate -i '{{ regexp.Split `[\s,.]` "foo bar,baz.qux" | toJSON}}'
   226  ["foo","bar","baz","qux"]
   227  ```
   228  ```console
   229  $ gomplate -i '{{ "foo bar.baz,qux" | regexp.Split `[\s,.]` 3 | toJSON}}'
   230  ["foo","bar","baz"]
   231  ```