]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: add matching and finding examples
authorVladimir Kovpak <cn007b@gmail.com>
Mon, 19 Nov 2018 22:11:33 +0000 (22:11 +0000)
committerIan Lance Taylor <iant@golang.org>
Mon, 19 Nov 2018 23:00:33 +0000 (23:00 +0000)
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>
src/regexp/example_test.go

index d65464665f77986ea2c946c6de6246e3a5a4badf..d134a6ba282db7f56c4afb8f916c578a9abb5647 100644 (file)
@@ -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"))