mirror of
https://github.com/golang/go
synced 2024-11-27 05:21:22 -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:
parent
547021d723
commit
2b6b474f64
@ -181,6 +181,19 @@ func ExampleRegexp_MatchString() {
|
|||||||
// true
|
// 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() {
|
func ExampleRegexp_ReplaceAllLiteralString() {
|
||||||
re := regexp.MustCompile(`a(x*)b`)
|
re := regexp.MustCompile(`a(x*)b`)
|
||||||
fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T"))
|
fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T"))
|
||||||
|
Loading…
Reference in New Issue
Block a user