Commit 10841494 authored by Vladimir Kovpak's avatar Vladimir Kovpak Committed by Ian Lance Taylor

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: 's avatarIan Lance Taylor <iant@golang.org>
parent 3d72ca99
......@@ -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"))
......
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