publish regular-expressions

This commit is contained in:
Mark McGranaghan 2012-10-30 17:54:20 -07:00
parent 300c1dc390
commit 789ce4af28
2 changed files with 72 additions and 21 deletions

View File

@ -4,37 +4,76 @@
package main
import "bytes"
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)
match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
fmt.Println(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")
// Above we used a string pattern directly, but for
// other regexp tasks you'll need to `Compile` an
// optimized `Regexp` struct.
r, _ := 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"))
fmt.Println(r.MatchString("peach"))
//
// This finds the match for the regexp.
fmt.Println(r.FindString("peach punch"))
// 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)
// The also finds the first match but returns the
// start and end indexes for the match instead of the
// matching text.
fmt.Println(r.FindStringIndex("peach punch"))
// The `Submatch` variants include information about
// both the whole-pattern matches and the submatches
// within those matches. For example this will return
// information for both `p([a-z]+)ch` and `([a-z]+)`.
fmt.Println(r.FindStringSubmatch("peach punch"))
// Similarly this will return information about the
// indexes of matches and submatches.
fmt.Println(r.FindStringSubmatchIndex("peach punch"))
// The `All` variants of these functions apply to all
// matches in the input, not just the first. For
// example to find all matches for a regexp.
fmt.Println(r.FindAllString("peach punch pinch", -1))
// Providing a non-negative integer as the second
// argument to these functions will limit the number
// of matches.
fmt.Println(r.FindAllString("peach punch pinch", 2))
// Our examples above had string arguments and used
// names like `MatchString`. We can also provide
// `[]byte` arguments and drop `String` from the
// function name.
fmt.Println(r.Match([]byte("peach")))
// When creating constants with regular expressions
// you can use the `MustCompile` variation of
// `Compile`. A plain `Compile` won't work for
// constants because it has 2 return values.
r = regexp.MustCompile("p([a-z]+)ch")
fmt.Println(r)
// The `regexp` package can also be used to replace
// subsets of strings with other values.
fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))
// The `Func` variant allows you to transform matched
// text with a given function.
in := []byte("a peach")
out := r.ReplaceAllFunc(in, bytes.ToUpper)
fmt.Println(string(out))
}
// todo:
// todo: gsub
// todo: Find(All)?(String)?(Submatch)?(Index)?
// 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

@ -1,4 +1,16 @@
$ go run regular-expressions.go
match: false
match: false
regex: p[a-z]+ch
true
true
peach
[0 5]
[peach ea]
[0 5 1 3]
[peach punch pinch]
[peach punch]
true
p([a-z]+)ch
a <fruit>
a PEACH
# For a complete reference on Go regular expressions check
# the [`regexp`](http://golang.org/pkg/regexp/) package docs.