Skip to content

Commit

Permalink
Merge pull request #834 from hairyhenderson/regexp-quotemeta-751
Browse files Browse the repository at this point in the history
Add function regexp.QuoteMeta
  • Loading branch information
Dave Henderson authored and GitHub committed May 7, 2020
2 parents cdc8412 + f607561 commit cf05110
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
15 changes: 15 additions & 0 deletions docs-src/content/functions/regexp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ funcs:
- |
$ gomplate -i '{{ if (.Env.USER | regexp.Match `^h`) }}username ({{.Env.USER}}) starts with h!{{end}}'
username (hairyhenderson) starts with h!
- name: regexp.QuoteMeta
description: |
Escapes all regular expression metacharacters in the input. The returned string is a regular expression matching the literal text.
This function provides the same behaviour as Go's
[`regexp.QuoteMeta`](https://golang.org/pkg/regexp/#Regexp.QuoteMeta) function.
pipeline: true
arguments:
- name: input
required: true
description: The input to escape
examples:
- |
$ gomplate -i '{{ `{hello}` | regexp.QuoteMeta }}'
\{hello\}
- name: regexp.Replace
description: |
Replaces matches of a regular expression with the replacement string.
Expand Down
41 changes: 35 additions & 6 deletions docs/content/functions/regexp.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ This function provides the same behaviour as Go's
### Usage

```go
regexp.FindAll expression [false] input
regexp.FindAll expression [n] input
```
```go
input | regexp.FindAll expression [false]
input | regexp.FindAll expression [n]
```

### Arguments

| name | description |
|------|-------------|
| `expression` | _(required)_ The regular expression |
| `false` | _(optional)_ The number of matches to return |
| `n` | _(optional)_ The number of matches to return |
| `input` | _(required)_ The input to search |

### Examples
Expand Down Expand Up @@ -113,6 +113,35 @@ $ gomplate -i '{{ if (.Env.USER | regexp.Match `^h`) }}username ({{.Env.USER}})
username (hairyhenderson) starts with h!
```

## `regexp.QuoteMeta`

Escapes all regular expression metacharacters in the input. The returned string is a regular expression matching the literal text.

This function provides the same behaviour as Go's
[`regexp.QuoteMeta`](https://golang.org/pkg/regexp/#Regexp.QuoteMeta) function.

### Usage

```go
regexp.QuoteMeta input
```
```go
input | regexp.QuoteMeta
```

### Arguments

| name | description |
|------|-------------|
| `input` | _(required)_ The input to escape |

### Examples

```console
$ gomplate -i '{{ `{hello}` | regexp.QuoteMeta }}'
\{hello\}
```

## `regexp.Replace`

Replaces matches of a regular expression with the replacement string.
Expand Down Expand Up @@ -205,18 +234,18 @@ This function provides the same behaviour as Go's
### Usage

```go
regexp.Split expression [false] input
regexp.Split expression [n] input
```
```go
input | regexp.Split expression [false]
input | regexp.Split expression [n]
```

### Arguments

| name | description |
|------|-------------|
| `expression` | _(required)_ The regular expression |
| `false` | _(optional)_ The number of matches to return |
| `n` | _(optional)_ The number of matches to return |
| `input` | _(required)_ The input to search |

### Examples
Expand Down
5 changes: 5 additions & 0 deletions funcs/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func (f *ReFuncs) Match(re, input interface{}) bool {
return regexp.Match(conv.ToString(re), conv.ToString(input))
}

// QuoteMeta -
func (f *ReFuncs) QuoteMeta(in interface{}) string {
return regexp.QuoteMeta(conv.ToString(in))
}

// Replace -
func (f *ReFuncs) Replace(re, replacement, input interface{}) string {
return regexp.Replace(conv.ToString(re),
Expand Down
5 changes: 5 additions & 0 deletions regexp/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func Match(expression, input string) bool {
return re.MatchString(input)
}

// QuoteMeta -
func QuoteMeta(input string) string {
return stdre.QuoteMeta(input)
}

// Replace -
func Replace(expression, replacement, input string) string {
re := stdre.MustCompile(expression)
Expand Down
4 changes: 4 additions & 0 deletions regexp/regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ func TestSplit(t *testing.T) {
assert.EqualValues(t, d.expected, f)
}
}

func TestQuoteMeta(t *testing.T) {
assert.Equal(t, `foo\{\(\\`, QuoteMeta(`foo{(\`))
}
6 changes: 6 additions & 0 deletions tests/integration/regexp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ func (s *RegexpSuite) TestReplace(c *C) {
`{{ "1.2.3-59" | regexp.Replace "-([0-9]*)" ".$1" }}`)
result.Assert(c, icmd.Expected{ExitCode: 0, Out: "1.2.3.59"})
}

func (s *RegexpSuite) TestQuoteMeta(c *C) {
result := icmd.RunCommand(GomplateBin, "-i",
"{{ regexp.QuoteMeta `foo{(\\` }}")
result.Assert(c, icmd.Expected{ExitCode: 0, Out: `foo\{\(\\`})
}

0 comments on commit cf05110

Please sign in to comment.