regexp functions

regexp.Replace

Replaces matches of a regular expression with the replacement string. The syntax of the regular expressions accepted is Go’s regexp syntax, and is the same general syntax used by Perl, Python, and other languages.

Usage

regexp.Replace expression replacement input
input | regexp.Replace expression replacement

Arguments

name description
expression The regular expression string
replacement The replacement string
input the input string to operate on

Examples

$ gomplate -i '{{ regexp.Replace "(foo)bar" "$1" "foobar"}}'
foo
$ gomplate -i '{{ regexp.Replace "(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)" "${last}, ${first}" "Alan Turing"}}'
Turing, Alan

regexp.Match

Returns true if a given regular expression matches a given input string.

This returns a boolean which can be used in an if condition, for example.

Usage

regexp.Match expression input
input | regexp.Match expression

Arguments

name description
expression the regular expression to match
input the input string to test

Examples

$ gomplate -i '{{ if (.Env.USER | regexp.Match `^h`) }}username ({{.Env.USER}}) starts with h!{{end}}'
username (hairyhenderson) starts with h!