From 9748e64fe552f484285ea6399329e6c8e696db63 Mon Sep 17 00:00:00 2001 From: Pantelis Sampaziotis Date: Wed, 25 Sep 2019 20:20:37 +0000 Subject: [PATCH] regexp: add examples for FindSubmatchIndex and Longest 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 TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/regexp/example_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/regexp/example_test.go b/src/regexp/example_test.go index 10eb736c7c..ea35a2e591 100644 --- a/src/regexp/example_test.go +++ b/src/regexp/example_test.go @@ -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")) -- 2.50.0