以下是一个示例代码,使用了正则表达式来判断密码是否符合要求:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    password := 'Abc123!@#' // 修改双引号为单引号
    if isValidPassword(password) {
        fmt.Println("Password is valid")
    } else {
        fmt.Println("Password is invalid")
    }
}

func isValidPassword(password string) bool {
    hasLetter := regexp.MustCompile('[a-zA-Z]').MatchString(password)
    hasDigit := regexp.MustCompile('[0-9]').MatchString(password)
    hasSpecialChar := regexp.MustCompile('[\W_]').MatchString(password)

    return hasLetter && hasDigit && hasSpecialChar
}

isValidPassword函数中,我们使用regexp.MustCompile函数创建了三个正则表达式,分别用来匹配字母、数字和特殊字符。然后我们使用MatchString方法对密码进行匹配,如果都匹配成功则返回true,否则返回false

Golang 密码验证:判断密码是否包含数字、字母和特殊字符

原文地址: https://www.cveoy.top/t/topic/nggO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录