]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: improve Regexp.ReplaceAll documentation and tests related to Expand part
authorEduard Bondarenko <eduardbcom@gmail.com>
Tue, 1 Aug 2023 14:48:16 +0000 (14:48 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 7 Aug 2023 00:22:53 +0000 (00:22 +0000)
For #40329

Change-Id: Ie0cb337545ce39cd169129227c45f7d2eaebc898
GitHub-Last-Rev: c017d4c7c1bc1f8cd39e6c70b60885cef1231dcd
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>

src/regexp/example_test.go
src/regexp/regexp.go

index 466b38b0fa2aa48e8ca3d59c3d554f1d22e3f1bc..707445f9ff02f70b463be4b359a08395f6c1a6bc 100644 (file)
@@ -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("$1W")))
        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:
        // -T-T-
        // --xx-
        // ---
        // -W-xxW-
+       // --xx-
+       // -W-xxW-
 }
 
 func ExampleRegexp_ReplaceAllLiteralString() {
@@ -252,11 +259,18 @@ func ExampleRegexp_ReplaceAllString() {
        fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1"))
        fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W"))
        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:
        // -T-T-
        // --xx-
        // ---
        // -W-xxW-
+       // --xx-
+       // -W-xxW-
 }
 
 func ExampleRegexp_ReplaceAllStringFunc() {
index 1c9b2fd4de43ee1499e84fc01f184f3dd0612f1c..3752b467c63e93d60fa445ed7d0c9cf8e0fd97e4 100644 (file)
@@ -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
-// with the replacement string repl. Inside repl, $ signs are interpreted as
-// in Expand, so for instance $1 represents the text of the first submatch.
+// with the replacement string repl.
+// Inside repl, $ signs are interpreted as in Expand.
 func (re *Regexp) ReplaceAllString(src, repl string) string {
        n := 2
        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
-// with the replacement text repl. Inside repl, $ signs are interpreted as
-// in Expand, so for instance $1 represents the text of the first submatch.
+// with the replacement text repl.
+// Inside repl, $ signs are interpreted as in Expand.
 func (re *Regexp) ReplaceAll(src, repl []byte) []byte {
        n := 2
        if bytes.IndexByte(repl, '$') >= 0 {