gobyexample/regex/regex.go

26 lines
945 B
Go
Raw Permalink Normal View History

2024-05-01 16:32:47 +00:00
package regex
import (
"fmt"
"regexp"
"strings"
)
var r = regexp.MustCompile("p([a-z]+)ch")
var f = fmt.Println
func Regex() {
matches, _ := regexp.MatchString("p([a-z]+)ch", "peach")
f("regexp.MatchString =>", matches)
f("r.MatchString() =>", r.MatchString("peach"))
f("r.FindString() =>", r.FindString("peach poach pouch"))
f("r.FindAllString() =>", r.FindAllString("peach poach pouch", -1))
f("r.FindStringIndex() =>", r.FindStringIndex("peach poach pouch"))
f("r.FindAllStringIndex() =>", r.FindAllStringIndex("peach poach pouch", -1))
f("r.FindStringSubmatch() =>", r.FindStringSubmatch("peach poach pouch"))
f("r.FindAllStringSubmatch() =>", r.FindAllStringSubmatch("peach poach pouch", -1))
f("r.ReplaceAllString() =>", r.ReplaceAllString("peach poach pouch", "<fruit>"))
f("r.ReplaceAllStringFunc() =>", r.ReplaceAllStringFunc("peachy peter poached a pouch", func(sub string) string { return strings.ToUpper(sub) }))
}