From 108414946ee29d9c997354494a808b86bdd0c209 Mon Sep 17 00:00:00 2001 From: Vladimir Kovpak Date: Mon, 19 Nov 2018 22:11:33 +0000 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/regexp/example_test.go | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) 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 + // false + // 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")) -- 2.48.1