当前位置:网站首页>golang实现正则匹配:密码包含至少一位数字,字母和特殊字符,且长度8-16
golang实现正则匹配:密码包含至少一位数字,字母和特殊字符,且长度8-16
2022-04-23 06:15:00 【玩哈哈527】
golang正则匹配密码包含至少一位数字,字母和特殊字符,且长度8-16
前言
由于golang不支持预查之类的正则匹配规则,regex包不支持?=之类的格式,复杂的正则匹配难以实现,可以使用开源包regex2
一、安装包go get github.com/dlclark/regexp2
二、使用
代码如下(示例):
func matchStr(str string) string {
expr := `^(?![0-9a-zA-Z]+$)(?![a-zA-Z!@#$%^&*]+$)(?![0-9!@#$%^&*]+$)[0-9A-Za-z!@#$%^&*]{8,16}$`
reg, _ := regexp2.Compile(expr, 0)
m, _ := reg.FindStringMatch(str)
if m != nil {
res:= m.String()
return res
}
return "密码包含至少一位数字,字母和特殊字符,且长度8-16"
}
三、解释
“^(?![0-9a-zA-Z]+$)(?![a-zA-Z!@#$%^&*]+$)(?![0-9!@#$%^&*]+$)[0-9A-Za-z!@#$%^&*]{8,16}$”中:(?![0-9a-zA-Z]+$) 表示匹配后面非数字,非字母的字符
(?![a-zA-Z!@#$%^&*]+$) 表示匹配后面非字母,非特殊字符的字符
(?![0-9!@#$%^&*]+$) 表示匹配后面非数字,非特殊字符的字符
[0-9A-Za-z!@#$%^&*]{8,16} 表示长度8-16,可以是任意的数字,字母和其中包含的特殊字符
版权声明
本文为[玩哈哈527]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_28058509/article/details/120976294
边栏推荐
猜你喜欢

Résolution du système

Chapter 8 generative deep learning

传输层重要知识(面试,复试,期末)

Are realrange and einsum really elegant

带您遨游太空,美摄科技为航天创意小程序提供全面技术支持

rearrange 和 einsum 真的优雅吗

机器视觉系列(02)---TensorFlow2.3 + win10 + GPU安装
Raspberry Pie: two color LED lamp experiment

江宁医院DMR系统解决方案

【点云系列】DeepMapping: Unsupervised Map Estimation From Multiple Point Clouds
随机推荐
【无标题】PID控制TT编码器电机
F. The wonderful use of pad
How keras saves and loads the keras model
EasyUI combobox determines whether the input item exists in the drop-down list
AUTOSAR从入门到精通100讲(八十三)-BootLoader自我刷新
Intuitive understanding of torch nn. Unfold
基于51单片机的温湿度监测+定时报警系统(c51源码)
By onnx checker. check_ Common errors detected by model
CMSIS CM3源码注解
Solution to slow compilation speed of Xcode
Swin transformer to onnx
PyTorch 14. module类
PyTorch 10. Learning rate
Raspberry Pie: two color LED lamp experiment
广西电网|应急空天一体化通信系统方案
AUTOSAR从入门到精通100讲(八十四)-UDS之时间参数总结篇
PyTorch 13. Nested functions and closures (dog head)
GIS实用小技巧(三)-CASS怎么添加图例?
UEFI学习01-ARM AARCH64编译、ArmPlatformPriPeiCore(SEC)
【技术规范】:如何写好技术文档?