]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: add example for ReplaceAll
authorpsampaz <psampaz@gmail.com>
Tue, 6 Aug 2019 16:56:33 +0000 (16:56 +0000)
committerIan Lance Taylor <iant@golang.org>
Thu, 5 Sep 2019 23:52:39 +0000 (23:52 +0000)
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>
src/regexp/example_test.go

index a44c9396dedf4cb654622e570adeb6187fcb5beb..2d87580ecaedaad767962b2e0db902bd5aa85a7c 100644 (file)
@@ -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"))