228 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
		
		
			
		
	
	
			228 lines
		
	
	
		
			6.1 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
|  | package main | |||
|  | 
 | |||
|  | import ( | |||
|  | 	"fmt" | |||
|  | 	"regexp" | |||
|  | 	"time" | |||
|  | ) | |||
|  | 
 | |||
|  | func main() { | |||
|  | 	tests := []string{"明天下午三点", "每两天两个小时", "每天上午", "周五上午", "每周四下午三点", "2023年6月18日上午8点49", "大后天的16:00", "每隔5天提醒我吃饭", "从后天上午9点开始,每3天"} | |||
|  | 
 | |||
|  | 	for _, test := range tests { | |||
|  | 		fmt.Printf("测试文本:%s\n", test) | |||
|  | 
 | |||
|  | 		// 匹配时间模板 | |||
|  | 		pattern1 := `(?P<y>\d{4})年(?P<mo>\d{1,2})月(?P<d>\d{1,2})日(?P<h>\d{1,2}):(?P<mi>\d{1,2})` | |||
|  | 		pattern2 := `(每)?((?P<day>\d+)天)?((?P<hour>\d+)个小时)?((?P<min>\d+)分)?((?P<sec>\d+)秒)?(后|钟|日)?` | |||
|  | 		pattern3 := `(?P<week>周[一二三四五六日])?((?P<ampm>[上中下])*午)?(?P<hour2>\d{1,2}):?(?P<mi2>\d{0,2})` | |||
|  | 		pattern4 := `每((?P<day2>[天周])[一二三四五六七八九十]+)?(?P<hour3>\d{1,2}):(?P<mi3>\d{2})` | |||
|  | 		pattern5 := `(?P<date2>[今明后大]?[0-9]{0,2}年[0-9]{0,2}月[0-9]{0,2}日)?(?P<week2>周[一二三四五六日])?((?P<ampm2>[上中下])*午)?(?P<hour4>\d{1,2}):?(?P<mi4>\d{0,2})` | |||
|  | 
 | |||
|  | 		match1, _ := regexp.MatchString(pattern1, test) | |||
|  | 		match2, _ := regexp.MatchString(pattern2, test) | |||
|  | 		match3, _ := regexp.MatchString(pattern3, test) | |||
|  | 		match4, _ := regexp.MatchString(pattern4, test) | |||
|  | 		match5, _ := regexp.MatchString(pattern5, test) | |||
|  | 
 | |||
|  | 		// 判断是否存在循环提醒 | |||
|  | 		isLoop := false | |||
|  | 		if match2 { | |||
|  | 			isLoop = matchDayHourMinSecLoop(test) | |||
|  | 		} else if match4 { | |||
|  | 			isLoop = matchEveryDayLoop(test) | |||
|  | 		} else if match5 { | |||
|  | 			isLoop = matchStartLoop(test) | |||
|  | 		} | |||
|  | 
 | |||
|  | 		if match1 || match2 || match3 || match4 || match5 { | |||
|  | 			fmt.Println("匹配到时间!") | |||
|  | 			if isLoop { | |||
|  | 				fmt.Println("存在循环提醒!") | |||
|  | 			} else { | |||
|  | 				fmt.Println("不存在循环提醒!") | |||
|  | 			} | |||
|  | 		} else { | |||
|  | 			fmt.Println("未匹配到时间!") | |||
|  | 		} | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | // 判断每天、每两天、每小时、每分钟循环提醒 | |||
|  | func matchDayHourMinSecLoop(text string) bool { | |||
|  | 	pattern := `(每)?((?P<day>\d+)天)?((?P<hour>\d+)个小时)?((?P<min>\d+)分)?((?P<sec>\d+)秒)?(后|钟|日)?` | |||
|  | 	re := regexp.MustCompile(pattern) | |||
|  | 	match := re.FindStringSubmatch(text) | |||
|  | 
 | |||
|  | 	// 判断是否存在循环提醒 | |||
|  | 	for _, group := range match[1:] { | |||
|  | 		if len(group) > 0 { | |||
|  | 			return true | |||
|  | 		} | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return false | |||
|  | } | |||
|  | 
 | |||
|  | // 判断每天、每周循环提醒 | |||
|  | func matchEveryDayLoop(text string) bool { | |||
|  | 	pattern := `每((?P<day>[天周])[一二三四五六七八九十]+)?(?P<hour>\d{1,2}):(?P<mi>\d{2})` | |||
|  | 	re := regexp.MustCompile(pattern) | |||
|  | 	match := re.FindStringSubmatch(text) | |||
|  | 
 | |||
|  | 	if len(match[1]) > 0 || len(match[2]) > 0 { | |||
|  | 		return true | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return false | |||
|  | } | |||
|  | 
 | |||
|  | // 判断从某个时间开始,每隔多长时间循环提醒 | |||
|  | func matchStartLoop(text string) bool { | |||
|  | 	pattern := `(?P<date>[今明后大]?[0-9]{0,2}年[0-9]{0,2}月[0-9]{0,2}日)?(?P<week>周[一二三四五六日])?((?P<ampm>[上中下])*午)?(?P<hour>\d{1,2}):?(?P<mi>\d{0,2})开始,每隔(?P<loop>\d+)([天时分])(\d+)?(\w+)?(\d+秒)?` | |||
|  | 	re := regexp.MustCompile(pattern) | |||
|  | 	match := re.FindStringSubmatch(text) | |||
|  | 
 | |||
|  | 	loopDuration := parseDuration(match[2], match[3], match[4], match[5], match[6]) | |||
|  | 	if loopDuration > 0 { | |||
|  | 		return true | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return false | |||
|  | } | |||
|  | 
 | |||
|  | // 解析时间段 | |||
|  | func parseDuration(d, h, m, s, unit string) time.Duration { | |||
|  | 	duration := time.Duration(0) | |||
|  | 
 | |||
|  | 	if len(d) > 0 { | |||
|  | 		duration += 24 * time.Duration(parseInt(d)) * time.Hour | |||
|  | 	} | |||
|  | 
 | |||
|  | 	if len(h) > 0 { | |||
|  | 		duration += time.Duration(parseInt(h)) * time.Hour | |||
|  | 	} | |||
|  | 
 | |||
|  | 	if len(m) > 0 { | |||
|  | 		duration += time.Duration(parseInt(m)) * time.Minute | |||
|  | 	} | |||
|  | 
 | |||
|  | 	if len(s) > 0 { | |||
|  | 		duration += time.Duration(parseInt(s)) * time.Second | |||
|  | 	} | |||
|  | 
 | |||
|  | 	switch unit { | |||
|  | 	case "时": | |||
|  | 		duration += time.Hour | |||
|  | 	case "分": | |||
|  | 		duration += time.Minute | |||
|  | 	case "秒": | |||
|  | 		duration += time.Second | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return duration | |||
|  | } | |||
|  | 
 | |||
|  | // 将中文数字转换为整数 | |||
|  | func parseInt(chineseNum string) int { | |||
|  | 	chineseNums := map[string]int{ | |||
|  | 		"零": 0, | |||
|  | 		"一": 1, | |||
|  | 		"二": 2, | |||
|  | 		"三": 3, | |||
|  | 		"四": 4, | |||
|  | 		"五": 5, | |||
|  | 		"六": 6, | |||
|  | 		"七": 7, | |||
|  | 		"八": 8, | |||
|  | 		"九": 9, | |||
|  | 		"十": 10, | |||
|  | 		"两": 2, // 特殊情况,"两"表示2 | |||
|  | 	} | |||
|  | 
 | |||
|  | 	num := 0 | |||
|  | 	for i := 0; i < len(chineseNum); i++ { | |||
|  | 		curNum := chineseNums[string(chineseNum[i])] | |||
|  | 		if curNum == 10 { | |||
|  | 			if num == 0 { | |||
|  | 				num = 10 | |||
|  | 			} else { | |||
|  | 				num *= 10 | |||
|  | 			} | |||
|  | 		} else { | |||
|  | 			num += curNum | |||
|  | 		} | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return num | |||
|  | } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | package main | |||
|  | 
 | |||
|  | import ( | |||
|  | 	"fmt" | |||
|  | 	"regexp" | |||
|  | ) | |||
|  | 
 | |||
|  | func main() { | |||
|  | 	tests := []string{"明天下午三点五十八秒", "每两天两个小时十分三秒", "每天上午零点", "周五上午一时", "每周四下午三点四十二秒", "2023年6月18日上午8点49分58秒", "大后天的16:00:23", "每隔5天提醒我吃饭", "从后天上午9点开始,每3天"} | |||
|  | 
 | |||
|  | 	// 模式1:日期时间模式,如:2021年8月1日下午3点20分 | |||
|  | 	pattern1 := `(\d{4}年\d{1,2}月\d{1,2}日)?([上中下午夜早]+)?(\d{1,2}[点时](\d{1,2}分)?(\d{1,2}秒)?)?` | |||
|  | 
 | |||
|  | 	// 模式2:大后天模式,如:大后天的16:00 | |||
|  | 	pattern2 := `[大明后](前|后)?天的(\d{1,2}:[0-5]\d:[0-5]\d)` | |||
|  | 
 | |||
|  | 	// 模式3:每隔多长时间模式,如:每两天两个小时,每隔5天提醒我吃饭 | |||
|  | 	pattern3 := `每隔(\d+)([天时分])(\d+)?(\w+)?(\d+秒)?` | |||
|  | 
 | |||
|  | 	// 模式4:频率模式,如:每天上午,周五上午,每周四下午三点 | |||
|  | 	pattern4 := `每([天周])?([一二三四五六七八九十]+)日?([上中下午夜早]+)?(\d{1,2}:[0-5]\d(:[0-5]\d)?)?` | |||
|  | 
 | |||
|  | 	// 模式5:从某个日期时间开始,每隔多长时间,如:从后天上午9点开始,每3天 | |||
|  | 	pattern5 := `从([大明后](前|后)?天的|今天|明天|后天|(\d{4}年\d{1,2}月\d{1,2}日))(\d{1,2}:[0-5]\d(:[0-5]\d)?)?开始,每隔(\d+)([天时分])(\d+)?(\w+)?(\d+秒)?` | |||
|  | 
 | |||
|  | 	for _, test := range tests { | |||
|  | 		fmt.Printf("测试文本:%s\n", test) | |||
|  | 
 | |||
|  | 		match1, _ := regexp.MatchString(pattern1, test) | |||
|  | 		if match1 { | |||
|  | 			fmt.Println("匹配到时间!") | |||
|  | 		} else { | |||
|  | 			fmt.Println("未匹配到时间!") | |||
|  | 		} | |||
|  | 
 | |||
|  | 		match2, _ := regexp.MatchString(pattern2, test) | |||
|  | 		if match2 { | |||
|  | 			fmt.Println("匹配到大后天!") | |||
|  | 		} | |||
|  | 
 | |||
|  | 		match3, _ := regexp.MatchString(pattern3, test) | |||
|  | 		if match3 { | |||
|  | 			fmt.Println("匹配到每隔时间!") | |||
|  | 		} | |||
|  | 
 | |||
|  | 		match4, _ := regexp.MatchString(pattern4, test) | |||
|  | 		if match4 { | |||
|  | 			fmt.Println("匹配到频率时间!") | |||
|  | 		} | |||
|  | 
 | |||
|  | 		match5, _ := regexp.MatchString(pattern5, test) | |||
|  | 		if match5 { | |||
|  | 			fmt.Println("匹配到从某个时间开始,每隔多长时间!") | |||
|  | 		} | |||
|  | 
 | |||
|  | 		fmt.Println("") | |||
|  | 	} | |||
|  | } |