1
0
mirror of https://github.com/golang/go synced 2024-11-23 10:00:03 -07:00

regexp: add example for ReplaceAll

Updates #21450

Change-Id: Ia31c20b52bae5daeb33d918234c2f0944a8aeb07
GitHub-Last-Rev: cc85544770
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>
This commit is contained in:
psampaz 2019-08-06 16:56:33 +00:00 committed by Ian Lance Taylor
parent 547021d723
commit 2b6b474f64

View 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"))