Created
July 15, 2020 06:39
-
-
Save skyjia/a727df89b2473c5b83b18da696804e51 to your computer and use it in GitHub Desktop.
Revisions
-
skyjia created this gist
Jul 15, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ package main import ( "fmt" "regexp" ) func main() { e := `^(?P<length>\d+)(?P<unit>[dmy])$` r := regexp.MustCompile(e) names := r.SubexpNames() tests := []string{ `1234d`, `2y`, `234m`, `1234`, `mmmm`} for _, t := range tests { fmt.Printf("testing: %q\r\n", t) result := r.FindAllStringSubmatch(t, -1) matched := r.MatchString(t) fmt.Println("matched:", matched) if result != nil { m := map[string]string{} for i, n := range result[0] { m[names[i]] = n } fmt.Printf("<length>: %q\r\n", m["length"]) fmt.Printf("<unit>: %q\r\n", m["unit"]) } fmt.Println() } }