From 1fbfe779e6758d0f7e9143ea2a00ad050ab8c6aa Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Tue, 13 Jun 2017 22:05:52 -0400 Subject: Adding regexp support Signed-off-by: Dave Henderson --- docs/content/functions/regexp.md | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 docs/content/functions/regexp.md (limited to 'docs') diff --git a/docs/content/functions/regexp.md b/docs/content/functions/regexp.md new file mode 100644 index 00000000..c97a81a8 --- /dev/null +++ b/docs/content/functions/regexp.md @@ -0,0 +1,70 @@ +--- +title: regexp functions +menu: + main: + parent: 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](https://golang.org/pkg/regexp/syntax/#hdr-Syntax), +and is the same general syntax used by Perl, Python, and other languages. + +### Usage + +```go +regexp.Replace expression replacement input +``` +```go +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 + +```console +$ gomplate -i '{{ regexp.Replace "(foo)bar" "$1" "foobar"}}' +foo +``` + +```console +$ gomplate -i '{{ regexp.Replace "(?P[a-zA-Z]+) (?P[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 + +```go +regexp.Match expression input +``` +```go +input | regexp.Match expression +``` + +### Arguments + +| name | description | +|--------|-------| +| `expression` | the regular expression to match | +| `input` | the input string to test | + +### Examples + +```console +$ gomplate -i '{{ if (.Env.USER | regexp.Match `^h`) }}username ({{.Env.USER}}) starts with h!{{end}}' +username (hairyhenderson) starts with h! +``` -- cgit v1.2.3