diff options
author | Liz Rice <liz@lizrice.com> | 2018-06-20 20:33:37 +0100 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2019-09-18 20:26:36 +0000 |
commit | 5253a6dc72c427cfeeabdce6d95c37c0716ab451 (patch) | |
tree | d052ea8246281df5590e158ac303c8a6af7e691e /src/regexp | |
parent | 09824ccfe5dce385e4f02894883b635a52a30cac (diff) | |
download | go-5253a6dc72c427cfeeabdce6d95c37c0716ab451.tar.gz go-5253a6dc72c427cfeeabdce6d95c37c0716ab451.zip |
regexp: add more examples for Regexp methods
Since I first started on this CL, most of the methods have had examples
added by other folks, so this is now one new example, and additions to
two existing examples for extra clarity.
The issue has a comment about not necessarily having examples for all
methods, but I recall finding this package pretty confusing when I first
used it, and having concrete examples would have really helped me
navigate all the different options. There are more
String methods with examples now, but I think seeing how the byte-slice
methods work could also be helpful to explain the differences.
Updates #21450
Change-Id: I27b4eeb634fb8ab59f791c0961cce79a67889826
Reviewed-on: https://go-review.googlesource.com/c/go/+/120145
Reviewed-by: Daniel Martà <mvdan@mvdan.cc>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Daniel Martà <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/regexp')
-rw-r--r-- | src/regexp/example_test.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/regexp/example_test.go b/src/regexp/example_test.go index 57b18e3fd7..10eb736c7c 100644 --- a/src/regexp/example_test.go +++ b/src/regexp/example_test.go @@ -94,9 +94,11 @@ func ExampleRegexp_FindSubmatch() { func ExampleRegexp_Match() { re := regexp.MustCompile(`foo.?`) fmt.Println(re.Match([]byte(`seafood fool`))) + fmt.Println(re.Match([]byte(`something else`))) // Output: // true + // false } func ExampleRegexp_FindString() { @@ -182,9 +184,13 @@ func ExampleRegexp_MatchString() { } func ExampleRegexp_NumSubexp() { + re0 := regexp.MustCompile(`a.`) + fmt.Printf("%d\n", re0.NumSubexp()) + re := regexp.MustCompile(`(.*)((a)b)(.*)a`) fmt.Println(re.NumSubexp()) // Output: + // 0 // 4 } @@ -348,6 +354,7 @@ func ExampleRegexp_FindIndex() { // [18 33] // option1: value1 } + func ExampleRegexp_FindAllSubmatchIndex() { content := []byte(` # comment line @@ -373,3 +380,13 @@ func ExampleRegexp_FindAllSubmatchIndex() { // option2 // value2 } + +func ExampleRegexp_FindAllIndex() { + content := []byte("London") + re := regexp.MustCompile(`o.`) + fmt.Println(re.FindAllIndex(content, 1)) + fmt.Println(re.FindAllIndex(content, -1)) + // Output: + // [[1 3]] + // [[1 3] [4 6]] +} |