]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: add simple package-level example
authorAndrew Gerrand <adg@golang.org>
Wed, 14 Nov 2012 09:43:21 +0000 (10:43 +0100)
committerAndrew Gerrand <adg@golang.org>
Wed, 14 Nov 2012 09:43:21 +0000 (10:43 +0100)
Update #4125

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6846045

src/pkg/regexp/example_test.go [new file with mode: 0644]

diff --git a/src/pkg/regexp/example_test.go b/src/pkg/regexp/example_test.go
new file mode 100644 (file)
index 0000000..aa92e0b
--- /dev/null
@@ -0,0 +1,22 @@
+package regexp_test
+
+import (
+       "fmt"
+       "regexp"
+)
+
+func Example() {
+       // Compile the expression once, usually at init time.
+       // Use raw strings to avoid having to quote the backslashes.
+       var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`)
+
+       fmt.Println(validID.MatchString("adam[23]"))
+       fmt.Println(validID.MatchString("eve[7]"))
+       fmt.Println(validID.MatchString("Job[48]"))
+       fmt.Println(validID.MatchString("snakey"))
+       // Output:
+       // true
+       // true
+       // false
+       // false
+}