summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpsampaz <psampaz@gmail.com>2019-08-06 16:56:33 +0000
committerIan Lance Taylor <iant@golang.org>2019-09-05 23:52:39 +0000
commit2b6b474f64d8f21d911910b04ef5a806ee37b154 (patch)
tree4dadcb28d3389ff97c6e5e81fb31af8c2673ea8b /src
parent547021d723364451f5a248d6d42da7d9f67bf7a6 (diff)
downloadgo-git-2b6b474f64d8f21d911910b04ef5a806ee37b154.tar.gz
regexp: add example for ReplaceAll
Updates #21450 Change-Id: Ia31c20b52bae5daeb33d918234c2f0944a8aeb07 GitHub-Last-Rev: cc8554477024277c3c1b4122344e9d14427680b3 GitHub-Pull-Request: golang/go#33489 Reviewed-on: https://go-review.googlesource.com/c/go/+/189137 Run-TryBot: Sylvain Zimmer <sylvinus@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/regexp/example_test.go13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/regexp/example_test.go b/src/regexp/example_test.go
index a44c9396de..2d87580eca 100644
--- a/src/regexp/example_test.go
+++ b/src/regexp/example_test.go
@@ -181,6 +181,19 @@ func ExampleRegexp_MatchString() {
// true
}
+func ExampleRegexp_ReplaceAll() {
+ re := regexp.MustCompile(`a(x*)b`)
+ fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("T")))
+ fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1")))
+ fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1W")))
+ fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W")))
+ // Output:
+ // -T-T-
+ // --xx-
+ // ---
+ // -W-xxW-
+}
+
func ExampleRegexp_ReplaceAllLiteralString() {
re := regexp.MustCompile(`a(x*)b`)
fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T"))