go语言正则表达式(匹配中文/匹配汉字)
package main
import (
"fmt"
"regexp"
)
func main() {
reg, err := regexp.Compile("[a-z0-9#$%&]+")
if err != nil {
fmt.Println(err)
}
fmt.Println(reg.MatchString("AIh"))
fmt.Println(reg.MatchString("an82&#"))
}
运行结果
false
true
第一个字符串AIh不匹配,第二个an82&#匹配。传入函数(re *Regexp) MatchString(s string) bool的字符串的每一个字符都会被检验是否属于[a-z0-9#$%&]其中的一个,a-z表示从小写a到小写z的26个英文字母,0-9表示从0到9的10个数字,#$%&是四个特殊字符,AIh中有两个大写字母,一个小写字母,h属于a-z,但字母A和I都不属于a-z,也不属于0-9,也不属于特殊字符,所以第一个不匹配,只要一段内容中有一个字符不匹配[a-z0-9#$%&]+,就表示该段内容不匹配,中括号外面的加号+表示多个匹配,即要求每一个字符都属于小写字母或数字,或四个特殊字符中的一个;[a-z0-7#$%&]去掉加号,表示某个字符串中只要有一个字符匹配,就表示匹配,每一个字符都不匹配,才表示不匹配。
reg, err := regexp.Compile("[a-z0-7#$%&]")
if err != nil {
fmt.Println(err)
}
fmt.Println(reg.MatchString("AI"))
fmt.Println(reg.MatchString("an82&#"))
fmt.Println(reg.MatchString("A!+"))
fmt.Println(reg.MatchString("aA!+"))
fmt.Println(reg.MatchString(strconv.Itoa(8)))
fmt.Println(reg.MatchString(strconv.Itoa(789)))
结果
false
true
false
true
false
true
该函数比regexp.Compile少一个返回值error,除此之外用法一样
package main
import (
"fmt"
"regexp"
"strconv"
)
func main() {
s := "日本"
s2 := "中国"
s3 := "ad"
s4 := "G"
s5 := 9
s6 := 708
s7 := "@"
s8 := "国8h+¥œ"
s9 := "%"
s10 := "^"
ss := make([]string, 0)
ss = append(ss, s, s2, s3, s4, strconv.Itoa(s5), strconv.Itoa(s6), s7, s8, s9, s10)
reg := regexp.MustCompile("^[a-zA-Z0-8中国!@#&*+_¥œø]+$")
for k, v := range ss {
fmt.Println(k, v, reg.MatchString(v))
}
}
运行结果
0 日本 false
1 中国 true
2 ad true
3 G true
4 9 false
5 708 true
6 @ true
7 国8h+¥œ true
8 % false
9 ^ false
函数Compile(expr string) (*Regexp, error)和MustCompile(str string) *Regexp的参数是正则表达式;
函数(re *Regexp) MatchString(s string) bool的参数是需要检验的内容,
正则表达式"^[a-zA-Z0-9\u4e00-\u9fa5]{3,8}$",匹配小写字母、大写字母、数字、或中文,长度3到8位。
package main
import (
"fmt"
"regexp"
)
func main() {
reg, err := regexp.Compile("^[a-zA-Z0-9\u4e00-\u9fa5]{3,8}$")
if err != nil {
fmt.Println(err)
}
fmt.Println(reg.MatchString("春暖花开"))
fmt.Println(reg.MatchString("春暖"))
fmt.Println(reg.MatchString("568"))
fmt.Println(reg.MatchString("aingege"))
fmt.Println(reg.MatchString("EIOGNE"))
fmt.Println(reg.MatchString("DIfin梅6"))
}
运行结果
true
false
true
true
true
true
注意:函数Compile和MustCompile传入参数时要写在英文双引号里面,不可以是单引号,也不可以是特殊字符 ` ,就是esc键底下那个键,在匹配中文时这个符号和\u不匹配,会报错。
package main
import (
"fmt"
"regexp"
)
func main() {
//reg, err := regexp.Compile(`^[a-zA-Z0-9\u4e00-\u9fa5]{3,8}$`)
reg := regexp.MustCompile(`^[a-zA-Z0-9\u4e00-\u9fa5]{3,8}$`)
//reg := regexp.MustCompile("^[a-zA-Z0-9\u4e00-\u9fa5]{3,8}$")
//reg, err := regexp.Compile("^[a-zA-Z0-9\u4e00-\u9fa5]{3,8}$")
//if err != nil {
// fmt.Println(err)
//}
fmt.Println(reg.MatchString("春暖花开"))
fmt.Println(reg.MatchString("春暖"))
fmt.Println(reg.MatchString("569你$kfa"))
fmt.Println(reg.MatchString("aingege"))
fmt.Println(reg.MatchString("EIOGNE"))
fmt.Println(reg.MatchString("DIfin梅6"))
}
运行结果
panic: regexp: Compile(`^[a-zA-Z0-9\u4e00-\u9fa5]{3,8}$`): error parsing regexp: invalid escape sequence: `\u`
在不匹配中文时,正则表达式可以写在特殊符号``中,但是匹配中文时,符号``和\u会冲突,所以最好统一写在英文双引号里,就不会报错。
https://blog.youkuaiyun.com/Charliewolf/article/details/84061643
本文详细介绍了Go语言中正则表达式的使用方法,包括regexp.Compile和regexp.MustCompile函数的应用,以及如何匹配特定字符集和中文字符。通过实例演示了不同正则表达式的匹配效果。
8551

被折叠的 条评论
为什么被折叠?



