gpt4 book ai didi

regex - 开始,尝试循环日期正则表达式结果

转载 作者:IT王子 更新时间:2023-10-29 02:24:02 27 4
gpt4 key购买 nike

进行 FindAllStringSubmatch 正则表达式匹配,包括不同日期格式的命名组。我在循环结果时遇到问题。注释掉的条件使它工作,但不是很干净,并且当我添加额外的匹配项时它会中断。我觉得我正在接近它错误并且想要一些重定向。谢谢/

package main

import (
"fmt"
"regexp"
"strings"
)

func main() {
text := "12/31/1956 31/11/1960"
reg := []string{`(?P<month>1[12])/(?P<day>\d\d)/(?P<year>19\d\d)`, `(?P<day>\d\d)/(?P<month>1[12])/(?P<year>19\d\d)`}
// Combine them, case ignored
regall := "(?i)" + strings.Join(reg, "|")
myExp := regexp.MustCompile(regall)
match := myExp.FindAllStringSubmatch(text, -1)
fmt.Println("Match", match, len(match))
fmt.Println("Names", myExp.SubexpNames(), len(myExp.SubexpNames()))
for i := 0; i < len(match); i++ {
result := make(map[string]string)
for j, name := range myExp.SubexpNames() {
result[name] = match[i][j]
//if (result["month"] != "" && result["day"] != "" && result["year"] != "") {
fmt.Println(match[i][0],i,j,result["month"] + "/" + result["day"] + "/" + result["year"])
//}
}
}
}

Results in:
Match [[12/31/1956 12 31 1956 ] [31/11/1960 31 11 1960]] 2
Names [ month day year day month year] 7
12/31/1956 0 0 //
12/31/1956 0 1 12//
12/31/1956 0 2 12/31/
12/31/1956 0 3 12/31/1956
12/31/1956 0 4 12//1956
12/31/1956 0 5 //1956
12/31/1956 0 6 //
31/11/1960 1 0 //
31/11/1960 1 1 //
31/11/1960 1 2 //
31/11/1960 1 3 //
31/11/1960 1 4 /31/
31/11/1960 1 5 11/31/
31/11/1960 1 6 11/31/1960

最佳答案

经过一些反射(reflection)和上面的帮助,在上述答案的帮助下,我想出了分别循环正则表达式以更好地处理重叠的命名捕获组我想出了这个:(为了他人的利益而发布,欢迎任何批评)

package main

import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
)

func main() {
month_d := `(?P<month>1[012]|0?[1-9])`
month_t := `(?P<month>jan(?:uary|.)?|feb(?:ruary|.)?|mar(?:ch|.)?|apr(?:il|.)?|may(.)|jun(?:e|.)?|jul(?:y|.)?|aug(?:ust|.)?|sep(?:tember|t|t.|.)?|oct(?:ober|.)?|nov(?:ember|.)?|dec(?:ember|.)?)`
day := `(?P<day>3[01]|[12][0-9]|[0]?[1-9])(?:\s)?(?:th|rd|nd|st)?`
year := `(?P<year>(?:19|20)?\d\d)`
sep_d := `[?:/.-]`
sep_t := `[?:,\s]`

text := "fedskjnkvdsj February 6 2004 sdffd Jan 12th 56 1/12/2000 2013/12/1 2099/12/5 1/12/1999 dsfjhfdhj"
regs := []string{
"(" + month_d + sep_d + day + sep_d + year + ")",
"(" + year + sep_d + month_d + sep_d + day + ")",
"(" + day + sep_d + month_d + sep_d + year + ")",
"(" + month_t + sep_t + day + sep_t + year + ")",
"(" + day + sep_t + month_t + sep_t + year + ")",
}
for i := 0; i < len(regs); i++ {
myExp, err := regexp.Compile("(?i)" + regs[i])
if err != nil {
fmt.Printf("There is a problem with your regexp.\n")
return
}
match := myExp.FindAllStringSubmatch(text, -1)
//fmt.Println("Match", match, len(match))
//fmt.Println("Names", myExp.SubexpNames(), len(myExp.SubexpNames()))
for j := 0; j < len(match); j++ {
result := make(map[string]string)
for k, name := range myExp.SubexpNames() {
result[name] = match[j][k]
}
// Fix Month
themonth := strings.ToLower(result["month"])

if len(themonth) == 1 {
themonth = "0" + themonth
}
if len(themonth) >= 3 {
themonth = themonth[0:3]
switch themonth {
case "jan":
themonth = "01"
case "feb":
themonth = "02"
case "mar":
themonth = "03"
case "apr":
themonth = "04"
case "may":
themonth = "05"
case "jun":
themonth = "06"
case "jul":
themonth = "07"
case "aug":
themonth = "08"
case "sep":
themonth = "09"
case "oct":
themonth = "10"
case "nov":
themonth = "11"
case "dec":
themonth = "12"
default:
fmt.Println("Month Error: " + themonth)
os.Exit(2)
}
}
// Day
theday := result["day"]
if len(theday) == 1 {
theday = "0" + theday
}
// Fix Year
theyear := result["year"]
if len(theyear) == 2 {
val_year, err := strconv.ParseInt(theyear,10,0)
if err != nil {
// handle error
fmt.Println(err)
os.Exit(2)
}
if val_year > 50 {
theyear = "19" + theyear
} else {
theyear = "20" + theyear
}
}
date := themonth + "/" + theday + "/" + theyear
fmt.Println(date)
}
}
}

关于regex - 开始,尝试循环日期正则表达式结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28823626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com