Commit 9748e64f authored by Pantelis Sampaziotis's avatar Pantelis Sampaziotis Committed by Ian Lance Taylor

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: default avatarIan Lance Taylor <iant@golang.org>
parent 2d1c0332
...@@ -172,6 +172,34 @@ func ExampleRegexp_FindAllStringSubmatchIndex() { ...@@ -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() { func ExampleRegexp_MatchString() {
re := regexp.MustCompile(`(gopher){2}`) re := regexp.MustCompile(`(gopher){2}`)
fmt.Println(re.MatchString("gopher")) fmt.Println(re.MatchString("gopher"))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment