From: psampaz Date: Tue, 6 Aug 2019 16:56:33 +0000 (+0000) Subject: regexp: add example for ReplaceAll X-Git-Tag: go1.14beta1~1194 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2b6b474f64d8f21d911910b04ef5a806ee37b154;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- 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"))