Bry*_*mas 3 regex go

我有一组表示持续时间的人类可读字符串.以下是四个例子:

1 days 40 hrs 23 min 50 sec

3 hrs 1 min 30 sec

10 days 23 min 11 sec

52 sec
[, ]
1 days 40 hrs 23 min 50 sec

是一个数组(或切片),如:

[[1, "days"], [40, "hrs"], [23, "min"], [50, "sec"]]
segs1 days[1, days]segs2
// time unit tokenizer
package main

import "fmt"
import "regexp"

func main() {
    s := "1 days 40 hrs 23 min 50 sec"
    re := regexp.MustCompile("(?P<quant>\\d+) (?P<unit>\\w+)+")

    segs := re.FindAllString(s, -1)
    fmt.Println("segs:", segs)
    fmt.Println(segs[0], "," ,segs[1], ",", segs[2], ",", segs[3])  
    fmt.Println("length segs:", len(segs))

    segs2 := re.FindAllStringSubmatch(s, -1)
    fmt.Println("segs2:", segs2)
    fmt.Println(segs2[0], "," ,segs2[1], ",", segs2[2], ",", segs2[3])
    fmt.Println("length segs2:", len(segs2))
}

输出:

segs: [1 days 40 hrs 23 min 50 sec]
1 days , 40 hrs , 23 min , 50 sec
length segs: 4
segs2: [[1 days 1 days] [40 hrs 40 hrs] [23 min 23 min] [50 sec 50 sec]]
[1 days 1 days] , [40 hrs 40 hrs] , [23 min 23 min] , [50 sec 50 sec]
length segs2: 4
re