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