mirror of
https://github.com/golang/go
synced 2024-11-17 16:44:44 -07:00
regexp: improve Regexp.ReplaceAll documentation and tests related to Expand part
For #40329
Change-Id: Ie0cb337545ce39cd169129227c45f7d2eaebc898
GitHub-Last-Rev: c017d4c7c1
GitHub-Pull-Request: golang/go#56507
Reviewed-on: https://go-review.googlesource.com/c/go/+/446836
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
7889ae3b12
commit
6ab8dfbe6b
@ -228,11 +228,18 @@ func ExampleRegexp_ReplaceAll() {
|
|||||||
fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1")))
|
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("$1W")))
|
||||||
fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W")))
|
fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W")))
|
||||||
|
|
||||||
|
re2 := regexp.MustCompile(`a(?P<1W>x*)b`)
|
||||||
|
fmt.Printf("%s\n", re2.ReplaceAll([]byte("-ab-axxb-"), []byte("$1W")))
|
||||||
|
fmt.Printf("%s\n", re2.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W")))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// -T-T-
|
// -T-T-
|
||||||
// --xx-
|
// --xx-
|
||||||
// ---
|
// ---
|
||||||
// -W-xxW-
|
// -W-xxW-
|
||||||
|
// --xx-
|
||||||
|
// -W-xxW-
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRegexp_ReplaceAllLiteralString() {
|
func ExampleRegexp_ReplaceAllLiteralString() {
|
||||||
@ -252,11 +259,18 @@ func ExampleRegexp_ReplaceAllString() {
|
|||||||
fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1"))
|
fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1"))
|
||||||
fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W"))
|
fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W"))
|
||||||
fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W"))
|
fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W"))
|
||||||
|
|
||||||
|
re2 := regexp.MustCompile(`a(?P<1W>x*)b`)
|
||||||
|
fmt.Printf("%s\n", re2.ReplaceAllString("-ab-axxb-", "$1W"))
|
||||||
|
fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W"))
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// -T-T-
|
// -T-T-
|
||||||
// --xx-
|
// --xx-
|
||||||
// ---
|
// ---
|
||||||
// -W-xxW-
|
// -W-xxW-
|
||||||
|
// --xx-
|
||||||
|
// -W-xxW-
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRegexp_ReplaceAllStringFunc() {
|
func ExampleRegexp_ReplaceAllStringFunc() {
|
||||||
|
@ -573,8 +573,8 @@ func Match(pattern string, b []byte) (matched bool, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReplaceAllString returns a copy of src, replacing matches of the Regexp
|
// ReplaceAllString returns a copy of src, replacing matches of the Regexp
|
||||||
// with the replacement string repl. Inside repl, $ signs are interpreted as
|
// with the replacement string repl.
|
||||||
// in Expand, so for instance $1 represents the text of the first submatch.
|
// Inside repl, $ signs are interpreted as in Expand.
|
||||||
func (re *Regexp) ReplaceAllString(src, repl string) string {
|
func (re *Regexp) ReplaceAllString(src, repl string) string {
|
||||||
n := 2
|
n := 2
|
||||||
if strings.Contains(repl, "$") {
|
if strings.Contains(repl, "$") {
|
||||||
@ -672,8 +672,8 @@ func (re *Regexp) replaceAll(bsrc []byte, src string, nmatch int, repl func(dst
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReplaceAll returns a copy of src, replacing matches of the Regexp
|
// ReplaceAll returns a copy of src, replacing matches of the Regexp
|
||||||
// with the replacement text repl. Inside repl, $ signs are interpreted as
|
// with the replacement text repl.
|
||||||
// in Expand, so for instance $1 represents the text of the first submatch.
|
// Inside repl, $ signs are interpreted as in Expand.
|
||||||
func (re *Regexp) ReplaceAll(src, repl []byte) []byte {
|
func (re *Regexp) ReplaceAll(src, repl []byte) []byte {
|
||||||
n := 2
|
n := 2
|
||||||
if bytes.IndexByte(repl, '$') >= 0 {
|
if bytes.IndexByte(repl, '$') >= 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user