package main import ( "fmt" "regexp" ) func main() { regex := regexp.MustCompile(`(?PIn this example, we are using `SubexpNames()` to retrieve the names of the capturing groups in the regular expression `(?P\w+)\s(?P \w+)`) match := regex.FindStringSubmatch("John Doe") for i, name := range regex.SubexpNames() { if i > 0 && name != "" { fmt.Printf("%v: %v\n", name, match[i]) } } }