package main import ( "fmt" "regexp" ) func main() { r := regexp.MustCompile(`(\w+) World`) match := r.FindStringSubmatch("Hello World") fmt.Println(match[0]) // prints "Hello World" fmt.Println(match[1]) // prints "Hello" }
package main import ( "fmt" "regexp" ) func main() { r := regexp.MustCompile(`(\d+)-(\d+)-(\d+)`) match := r.FindStringSubmatch("Today's date is 2022-04-05") for i := 0; i < len(match); i++ { fmt.Println(match[i]) } }In this example, we define a regular expression that captures three groups of digits separated by hyphens. We then search for this regular expression in the string "Today's date is 2022-04-05" using the `FindStringSubmatch` function. The returned slice contains four elements: the entire matched string ("2022-04-05") and the three submatches ("2022", "04", and "05"). The package library used for these examples is `regexp`, which is the standard Go package for regular expressions.