golang正则提取时间2023-04
可以使用如下正则表达式提取时间:
import (
"regexp"
)
func main() {
str := "Today is 2023-04-01."
re := regexp.MustCompile(`\d{4}-\d{2}`)
match := re.FindString(str)
fmt.Println(match)
}
输出:
2023-04
解释:
\d{4}:匹配4个数字,即年份。-:匹配中横线。\d{2}:匹配2个数字,即月份。():将匹配的内容分组。re.FindString(str):返回str中第一个匹配正则表达式的字符串。
原文地址: https://www.cveoy.top/t/topic/fgTd 著作权归作者所有。请勿转载和采集!