diff options
author | Pantelis Sampaziotis <psampaz@gmail.com> | 2019-09-25 20:20:37 +0000 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2019-09-28 00:37:35 +0000 |
commit | 9748e64fe552f484285ea6399329e6c8e696db63 (patch) | |
tree | 0c84e7d04bece5b8fee2f5de7d7734c3138fd6d6 /src/regexp | |
parent | 2d1c0332598c77097d53a1c2d564b12dcd644810 (diff) | |
download | go-9748e64fe552f484285ea6399329e6c8e696db63.tar.gz go-9748e64fe552f484285ea6399329e6c8e696db63.zip |
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/regexp')
-rw-r--r-- | src/regexp/example_test.go | 28 |
1 files changed, 28 insertions, 0 deletions
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")) |