aboutsummaryrefslogtreecommitdiff
path: root/src/regexp
diff options
context:
space:
mode:
authorVladimir Kovpak <cn007b@gmail.com>2018-11-19 22:11:33 +0000
committerIan Lance Taylor <iant@golang.org>2018-11-19 23:00:33 +0000
commit108414946ee29d9c997354494a808b86bdd0c209 (patch)
tree80f21f2cfda6e9695d32753debd6d07a2435556e /src/regexp
parent3d72ca9908ba2696e1acafa7d7f8fee21131fe5e (diff)
downloadgo-108414946ee29d9c997354494a808b86bdd0c209.tar.gz
go-108414946ee29d9c997354494a808b86bdd0c209.zip
regexp: add matching and finding examples
This commit adds examples for Match, Find, FindAllSubmatch, FindSubmatch and Match functions. Change-Id: I2bdf8c3cee6e89d618109397378c1fc91aaf1dfb GitHub-Last-Rev: 33f34b7adca2911a4fff9638c93e846fb0021465 GitHub-Pull-Request: golang/go#28837 Reviewed-on: https://go-review.googlesource.com/c/150020 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.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/regexp/example_test.go b/src/regexp/example_test.go
index d65464665f..d134a6ba28 100644
--- a/src/regexp/example_test.go
+++ b/src/regexp/example_test.go
@@ -25,6 +25,20 @@ func Example() {
// false
}
+func ExampleMatch() {
+ matched, err := regexp.Match(`foo.*`, []byte(`seafood`))
+ fmt.Println(matched, err)
+ matched, err = regexp.Match(`bar.*`, []byte(`seafood`))
+ fmt.Println(matched, err)
+ matched, err = regexp.Match(`a(b`, []byte(`seafood`))
+ fmt.Println(matched, err)
+
+ // Output:
+ // true <nil>
+ // false <nil>
+ // false error parsing regexp: missing closing ): `a(b`
+}
+
func ExampleMatchString() {
matched, err := regexp.MatchString("foo.*", "seafood")
fmt.Println(matched, err)
@@ -44,6 +58,46 @@ func ExampleQuoteMeta() {
// Escaping symbols like: \.\+\*\?\(\)\|\[\]\{\}\^\$
}
+func ExampleRegexp_Find() {
+ re := regexp.MustCompile("foo.?")
+ fmt.Printf("%q\n", re.Find([]byte(`seafood fool`)))
+
+ // Output:
+ // "food"
+}
+
+func ExampleRegexp_FindAll() {
+ re := regexp.MustCompile("foo.?")
+ fmt.Printf("%q\n", re.FindAll([]byte(`seafood fool`), -1))
+
+ // Output:
+ // ["food" "fool"]
+}
+
+func ExampleRegexp_FindAllSubmatch() {
+ re := regexp.MustCompile("foo(.?)")
+ fmt.Printf("%q\n", re.FindAllSubmatch([]byte(`seafood fool`), -1))
+
+ // Output:
+ // [["food" "d"] ["fool" "l"]]
+}
+
+func ExampleRegexp_FindSubmatch() {
+ re := regexp.MustCompile("foo(.?)")
+ fmt.Printf("%q\n", re.FindSubmatch([]byte(`seafood fool`)))
+
+ // Output:
+ // ["food" "d"]
+}
+
+func ExampleRegexp_Match() {
+ re := regexp.MustCompile("foo.?")
+ fmt.Println(re.Match([]byte(`seafood fool`)))
+
+ // Output:
+ // true
+}
+
func ExampleRegexp_FindString() {
re := regexp.MustCompile("foo.?")
fmt.Printf("%q\n", re.FindString("seafood fool"))