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

     1  ns: regexp
     2  preamble: |
     3    These functions allow user you to search and modify text with regular expressions.
     4  
     5    The syntax of the regular expressions accepted is [Go's `regexp` syntax](https://golang.org/pkg/regexp/syntax/#hdr-Syntax),
     6    and is the same general syntax used by Perl, Python, and other languages.
     7  funcs:
     8    - name: regexp.Find
     9      description: |
    10        Returns a string holding the text of the leftmost match in `input`
    11        of the regular expression `expression`.
    12  
    13        This function provides the same behaviour as Go's
    14        [`regexp.FindString`](https://golang.org/pkg/regexp/#Regexp.FindString) function.
    15      pipeline: true
    16      arguments:
    17        - name: expression
    18          required: true
    19          description: The regular expression
    20        - name: input
    21          required: true
    22          description: The input to search
    23      examples:
    24        - |
    25          $ gomplate -i '{{ regexp.Find "[a-z]{3}" "foobar"}}'
    26          foo
    27        - |
    28          $ gomplate -i 'no {{ "will not match" | regexp.Find "[0-9]" }}numbers'
    29          no numbers
    30    - name: regexp.FindAll
    31      description: |
    32        Returns a list of all successive matches of the regular expression.
    33  
    34        This can be called with 2 or 3 arguments. When called with 2 arguments, the
    35        `n` argument (number of matches) will be set to `-1`, causing all matches
    36        to be returned.
    37  
    38        This function provides the same behaviour as Go's
    39        [`regexp.FindAllString`](https://golang.org/pkg/regexp/#Regexp.FindAllString) function.
    40      pipeline: true
    41      arguments:
    42        - name: expression
    43          required: true
    44          description: The regular expression
    45        - name: n
    46          required: false
    47          description: The number of matches to return
    48        - name: input
    49          required: true
    50          description: The input to search
    51      examples:
    52        - |
    53          $ gomplate -i '{{ regexp.FindAll "[a-z]{3}" "foobar" | toJSON}}'
    54          ["foo", "bar"]
    55        - |
    56          $ gomplate -i '{{ "foo bar baz qux" | regexp.FindAll "[a-z]{3}" 3 | toJSON}}'
    57          ["foo", "bar", "baz"]
    58    - name: regexp.Match
    59      description: |
    60        Returns `true` if a given regular expression matches a given input.
    61  
    62        This returns a boolean which can be used in an `if` condition, for example.
    63      pipeline: true
    64      arguments:
    65        - name: expression
    66          required: true
    67          description: The regular expression
    68        - name: input
    69          required: true
    70          description: The input to test
    71      examples:
    72        - |
    73          $ gomplate -i '{{ if (.Env.USER | regexp.Match `^h`) }}username ({{.Env.USER}}) starts with h!{{end}}'
    74          username (hairyhenderson) starts with h!
    75    - name: regexp.Replace
    76      description: |
    77        Replaces matches of a regular expression with the replacement string.
    78  
    79        The replacement is substituted after expanding variables beginning with `$`.
    80  
    81        This function provides the same behaviour as Go's
    82        [`regexp.ReplaceAllString`](https://golang.org/pkg/regexp/#Regexp.ReplaceAllString) function.
    83      pipeline: true
    84      arguments:
    85        - name: expression
    86          required: true
    87          description: The regular expression string
    88        - name: replacement
    89          required: true
    90          description: The replacement string
    91        - name: input
    92          required: true
    93          description: The input string to operate on
    94      examples:
    95        - |
    96          $ gomplate -i '{{ regexp.Replace "(foo)bar" "$1" "foobar"}}'
    97          foo
    98        - |
    99          $ gomplate -i '{{ regexp.Replace "(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)" "${last}, ${first}" "Alan Turing"}}'
   100          Turing, Alan
   101    - name: regexp.ReplaceLiteral
   102      description: |
   103        Replaces matches of a regular expression with the replacement string.
   104  
   105        The replacement is substituted directly, without expanding variables
   106        beginning with `$`.
   107  
   108        This function provides the same behaviour as Go's
   109        [`regexp.ReplaceAllLiteralString`](https://golang.org/pkg/regexp/#Regexp.ReplaceAllLiteralString) function.
   110      pipeline: true
   111      arguments:
   112        - name: expression
   113          required: true
   114          description: The regular expression string
   115        - name: replacement
   116          required: true
   117          description: The replacement string
   118        - name: input
   119          required: true
   120          description: The input string to operate on
   121      examples:
   122        - |
   123          $ gomplate -i '{{ regexp.ReplaceLiteral "(foo)bar" "$1" "foobar"}}'
   124          $1
   125        - |
   126          $ gomplate -i '{{ `foo.bar,baz` | regexp.ReplaceLiteral `\W` `$` }}'
   127          foo$bar$baz
   128    - name: regexp.Split
   129      description: |
   130        Splits `input` into sub-strings, separated by the expression.
   131  
   132        This can be called with 2 or 3 arguments. When called with 2 arguments, the
   133        `n` argument (number of matches) will be set to `-1`, causing all sub-strings
   134        to be returned.
   135  
   136        This is equivalent to [`strings.SplitN`](../strings/#strings-splitn),
   137        except that regular expressions are supported.
   138  
   139        This function provides the same behaviour as Go's
   140        [`regexp.Split`](https://golang.org/pkg/regexp/#Regexp.Split) function.
   141      pipeline: true
   142      arguments:
   143        - name: expression
   144          required: true
   145          description: The regular expression
   146        - name: n
   147          required: false
   148          description: The number of matches to return
   149        - name: input
   150          required: true
   151          description: The input to search
   152      examples:
   153        - |
   154          $ gomplate -i '{{ regexp.Split `[\s,.]` "foo bar,baz.qux" | toJSON}}'
   155          ["foo","bar","baz","qux"]
   156        - |
   157          $ gomplate -i '{{ "foo bar.baz,qux" | regexp.Split `[\s,.]` 3 | toJSON}}'
   158          ["foo","bar","baz"]