]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: add examples for FindSubmatchIndex and Longest
authorPantelis Sampaziotis <psampaz@gmail.com>
Wed, 25 Sep 2019 20:20:37 +0000 (20:20 +0000)
committerIan Lance Taylor <iant@golang.org>
Sat, 28 Sep 2019 00:37:35 +0000 (00:37 +0000)
updates #21450

Change-Id: Ibffe0dadc1e1523c55cd5f5b8a69bc1c399a255d
GitHub-Last-Rev: 507f55508121a525de4d210e7ada1396ccaaf367
GitHub-Pull-Request: golang/go#33497
Reviewed-on: https://go-review.googlesource.com/c/go/+/189177
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/regexp/example_test.go

index 10eb736c7c49b390336624797645ee3d2fe1bb9b..ea35a2e5918e32256e9176263b3ed039fbc40f5b 100644 (file)
@@ -172,6 +172,34 @@ func ExampleRegexp_FindAllStringSubmatchIndex() {
        // []
 }
 
+func ExampleRegexp_FindSubmatchIndex() {
+       re := regexp.MustCompile(`a(x*)b`)
+       // Indices:
+       //    01234567   012345678
+       //    -ab-axb-   -axxb-ab-
+       fmt.Println(re.FindSubmatchIndex([]byte("-ab-")))
+       fmt.Println(re.FindSubmatchIndex([]byte("-axxb-")))
+       fmt.Println(re.FindSubmatchIndex([]byte("-ab-axb-")))
+       fmt.Println(re.FindSubmatchIndex([]byte("-axxb-ab-")))
+       fmt.Println(re.FindSubmatchIndex([]byte("-foo-")))
+       // Output:
+       // [1 3 2 2]
+       // [1 5 2 4]
+       // [1 3 2 2]
+       // [1 5 2 4]
+       // []
+}
+
+func ExampleRegexp_Longest() {
+       re := regexp.MustCompile(`a(|b)`)
+       fmt.Println(re.FindString("ab"))
+       re.Longest()
+       fmt.Println(re.FindString("ab"))
+       // Output:
+       // a
+       // ab
+}
+
 func ExampleRegexp_MatchString() {
        re := regexp.MustCompile(`(gopher){2}`)
        fmt.Println(re.MatchString("gopher"))