start regular-expressions

This commit is contained in:
Mark McGranaghan 2012-10-30 16:18:02 -07:00
parent 266b028c63
commit 300c1dc390
5 changed files with 45 additions and 25 deletions

View File

@ -45,7 +45,7 @@ Defer
Collection Functions
String Functions
# String Formatting
# Regexs
Regular Expressions
# Bytes
# JSON
# Time

View File

@ -1,19 +0,0 @@
package main
import "regexp"
import "fmt"
func main() {
m1, _ := regexp.MatchString("p[a-z]+ch", "apple")
m2, _ := regexp.MatchString("p[a-z]+ch", "peach")
fmt.Println(m1)
fmt.Println(m2)
r1, _ := regexp.Compile("p[a-z]+ch")
fmt.Println(r1.MatchString("apple"))
fmt.Println(r1.MatchString("peach"))
}
// todo: more
// todo: gsub with regexp
// todo: rename to "Regular Expressions"

View File

@ -1,5 +0,0 @@
$ go run regexs.go
false
true
false
true

View File

@ -0,0 +1,40 @@
// Go offers built-in support for [regular expressions](http://en.wikipedia.org/wiki/Regular_expression).
// Here are some examples of common regexp-related tasks
// in Go.
package main
import "fmt"
import "regexp"
func main() {
// This tests whether a pattern matches a string.
match, _ := regexp.MatchString("p[a-z]+ch", "apple")
fmt.Println("match:", match)
// In the above example we used a string pattern
// directly. For other regular expression tasks you'll
// need to `Compile` a `Regexp` struct.
r1, _ := regexp.Compile("p[a-z]+ch")
// Many methods are available on these structs. Here's
// a match test like we saw earlier.
fmt.Println("match:", r1.MatchString("apple"))
//
// When creating top-level constants with regular
// expressions, you can use the `MustCompile` variant
// of the `Compile` function we saw earlier. A plain
// `Compile` won't work for constants because it has 2
// return values.
cr := regexp.MustCompile("p[a-z]+ch")
fmt.Println("regex:", cr)
}
// todo:
// todo: gsub
// todo: Examples of regular expressions in #golang: https://gobyexample.com/regular-expressions One of the best areas for a "by example" approach IMO.

View File

@ -0,0 +1,4 @@
$ go run regular-expressions.go
match: false
match: false
regex: p[a-z]+ch