diff options
author | Matej Baćo <matejbaco@gmail.com> | 2017-08-28 12:41:27 +0200 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2017-08-28 15:34:19 +0000 |
commit | 1fe1512f50585d461dd9f41d8b373da5ed66c99b (patch) | |
tree | b75de043b5ad03a6abd068e80d6274c478ff6234 /src/regexp/example_test.go | |
parent | c195deb48cf09970c6bb77fad96926e0af102dcf (diff) | |
download | go-git-1fe1512f50585d461dd9f41d8b373da5ed66c99b.tar.gz |
regexp: examples for Regexp.Expand and Regexp.ExpandString functions
Current documentation lacks simple examples for functions Regexp.Expand
and Regexp.ExpandString whose usage is unclear from description alone.
This commit adds examples that demonstrate usage in practical way.
Fixes #21649
Change-Id: I7b2c06c8ab747f69a6578f0595bf0f3c742ac479
Reviewed-on: https://go-review.googlesource.com/59470
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/regexp/example_test.go')
-rw-r--r-- | src/regexp/example_test.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/regexp/example_test.go b/src/regexp/example_test.go index 2ac92d4382..0bf1f6bee7 100644 --- a/src/regexp/example_test.go +++ b/src/regexp/example_test.go @@ -179,3 +179,67 @@ func ExampleRegexp_Split() { // [pizza] // [pi a] } + +func ExampleRegexp_Expand() { + content := []byte(` + # comment line + option1: value1 + option2: value2 + + # another comment line + option3: value3 +`) + + // Regex pattern captures "key: value" pair from the content. + pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) + + // Template to convert "key: value" to "key=value" by + // referencing the values captured by the regex pattern. + template := []byte("$key=$value\n") + + result := []byte{} + + // For each match of the regex in the content. + for _, submatches := range pattern.FindAllSubmatchIndex(content, -1) { + // Apply the captured submatches to the template and append the output + // to the result. + result = pattern.Expand(result, template, content, submatches) + } + fmt.Println(string(result)) + // Output: + // option1=value1 + // option2=value2 + // option3=value3 +} + +func ExampleRegexp_ExpandString() { + content := ` + # comment line + option1: value1 + option2: value2 + + # another comment line + option3: value3 +` + + // Regex pattern captures "key: value" pair from the content. + pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`) + + // Template to convert "key: value" to "key=value" by + // referencing the values captured by the regex pattern. + template := "$key=$value\n" + + result := []byte{} + + // For each match of the regex in the content. + for _, submatches := range pattern.FindAllStringSubmatchIndex(content, -1) { + // Apply the captured submatches to the template and append the output + // to the result. + result = pattern.ExpandString(result, template, content, submatches) + } + fmt.Println(string(result)) + // Output: + // option1=value1 + // option2=value2 + // option3=value3 +} |