Regular expressions
Context
Section titled “Context”The regexp package provides regular expression search and match. It uses the RE2 syntax (no backtracking, guaranteed linear time).
Example
Section titled “Example”Match a pattern and extract submatches.
Code example
Section titled “Code example”package main
import ( "fmt" "regexp")
func main() { pattern := regexp.MustCompile(`(\w+)=(\d+)`) s := "age=30 name=Alice height=175"
matches := pattern.FindAllStringSubmatch(s, -1) for _, m := range matches { fmt.Printf("key=%s, value=%s\n", m[1], m[2]) }
// Check if matches fmt.Println(pattern.MatchString("foo=123"))}Output
Section titled “Output”key=age, value=30key=name, value=Alicekey=height, value=175true