regexp: add examples for FindSubmatchIndex and Longest

updates #21450

Change-Id: Ibffe0dadc1e1523c55cd5f5b8a69bc1c399a255d
GitHub-Last-Rev: 507f555081
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>
This commit is contained in:
Pantelis Sampaziotis 2019-09-25 20:20:37 +00:00 committed by Ian Lance Taylor
parent 2d1c033259
commit 9748e64fe5
1 changed files with 28 additions and 0 deletions

View 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"))