1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package main
import ( "fmt" "github.com/dlclark/regexp2" )
func Regexp2GroupMatch(m *regexp2.Match, re *regexp2.Regexp) [][]string { var matches [][]string for m != nil { var ret []string gps := m.Groups() for index, g := range gps { if index == 0 { continue } ret = append(ret, g.Captures[0].String()) } matches = append(matches, ret) m, _ = re.FindNextMatch(m) } return matches }
func CompileRegexp(regex string) (*regexp2.Regexp, error) { msgRegexp, e := regexp2.Compile(regex, 0) if e != nil { fmt.Println(e) } return msgRegexp, nil }
func main() { str := "2022-8-12 2023-8-11" expr := "(\\d{4})-(\\d{1,2})-(\\d{1,2})" reg, _ := CompileRegexp(expr) m, _ := reg.FindStringMatch(str) ret := Regexp2GroupMatch(m, reg) fmt.Println(ret) }
|