package main import ( "fmt" "regexp" "unicode" ) // checkDigitSuffix 等价原 \d+(?!\D+$) // 入参:完整字符串、一段数字的结束下标 // 返回:true = 符合条件(数字后面不全是字母) func checkDigitSuffix(s string, digitEnd int) bool { // 数字后面无任何字符,直接不符合 if digitEnd >= len(s) { return false } after := s[digitEnd:] // 遍历数字之后所有字符 allLetter := true for _, r := range after { // 判断是否大小写字母 if !unicode.IsLetter(r) { allLetter = false break } } // 不全是字母 → 返回true匹配 return !allLetter } func main() { pwdList := []string{"abc123", "123abc", "abcdef", "x99!yy", "789#"} digitRe := regexp.MustCompile(`\d+`) fmt.Println("密码\t匹配结果") fmt.Println("----------------") for _, pwd := range pwdList { // 找出所有数字段 allDigitIdx := digitRe.FindAllStringIndex(pwd, -1) matchFlag := false for _, idx := range allDigitIdx { endPos := idx[1] if checkDigitSuffix(pwd, endPos) { matchFlag = true break } } fmt.Printf("%s\t%t\n", pwd, matchFlag) } }