正则小笔记
正则小笔记
1.前言
自己的小笔记,大家可以忽视
2.内容
字符代表:
- . 任意一个字符
- s 一个空白符 S 一个非空白分
- w 一个数字,字母和下划线 W 一个非..
- d 一个数字 D 一个非数字
限制:
- 零次或者多次
- 至少一次或者一次以上(注意这两个区别)
- ? 0个或者1个
- | 或
- [] []里面任意字符一个
- {} 修饰出现的次数
- () 一个组,被group捕获
- (?:) 不为一个组
- .?和.+? 是非贪婪匹配 , .为贪婪匹配 。默认为贪婪模式,(注意这个模式的坑点)(所谓贪婪就是会一直匹配到字符串结束,而非贪婪只匹配到第一次出现符号正则时候)
例如:
"select * from admin where id =".$id."and password=".$password;
.($w+).;----->组里,匹配到$password
.?($w+).?;----->组里,匹配到$id
go代码例子:
// 是否含有数字
has_num,_ := regexp.MatchString(`\d`, password)
if has_num {
pass.Has_num = true
}
// 是否含有字母
has_char,_ := regexp.MatchString(`[a-zA-Z]`, password)
if has_char {
pass.Has_char = true
}
// 是否含有字母大小写
has_captial,_ := regexp.MatchString(`[a-z].*[A-Z]|[A-Z].*[a-z]`, password)
if has_captial {
pass.Has_capital = true
}
// 是否含特殊字符
has_special,_ := regexp.MatchString(`[^a-zA-Z0-9]`, password)
if has_special == false {
pass.Has_special = true
}