1
0
mirror of https://github.com/golang/go synced 2024-11-25 11:27:56 -07:00

regexp: add comment in example test describing tricky behavior

This commit is contained in:
Anuraag Agrawal 2022-11-29 13:54:50 +09:00
parent 0fd7be7ee5
commit 1e7b236f3f

View File

@ -226,6 +226,7 @@ func ExampleRegexp_ReplaceAll() {
re := regexp.MustCompile(`a(x*)b`) 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("T")))
fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1"))) fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1")))
// Tries to find the named group `$1W` which is not present, so empty.
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")))
// Output: // Output:
@ -250,6 +251,7 @@ func ExampleRegexp_ReplaceAllString() {
re := regexp.MustCompile(`a(x*)b`) re := regexp.MustCompile(`a(x*)b`)
fmt.Println(re.ReplaceAllString("-ab-axxb-", "T")) fmt.Println(re.ReplaceAllString("-ab-axxb-", "T"))
fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1")) fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1"))
// Tries to find the named group `$1W` which is not present, so empty.
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"))
// Output: // Output: