例1:
package main import ( "fmt" "regexp" ) func main() { s1 := "Z22WhkC9K$N#wRIbBitpQYUFJqd%d*QVz5Ir7j&CLqz3@FOC2x5CD6ZzvhNdk@5Vv1QU^DHbSc&nBs%4!94ngFH0BJ6GxtXg2$T" reg := regexp.MustCompile(`[^a-zA-Z0-9]`) fmt.Println(reg.FindAllString(s1, -1)) reg = regexp.MustCompile(`[a-zA-Z0-9]`) fmt.Println(reg.FindAllString(s1, -1)) reg = regexp.MustCompile(`[a-z]`) fmt.Println(reg.FindAllString(s1, -1)) reg = regexp.MustCompile(`[A-Z]`) fmt.Println(reg.FindAllString(s1, -1)) reg = regexp.MustCompile(`[0-9]`) fmt.Println(reg.FindAllString(s1, -1)) s2 := "河北省河南省hbshns湖北省hbshns湖南省" reg = regexp.MustCompile("[\u4e00-\u9fa5]") fmt.Println(reg.FindAllString(s2, -1)) reg = regexp.MustCompile(`[\p{Han}]+`) fmt.Printf("%q\n", reg.FindAllString(s2, -1)) reg = regexp.MustCompile(`[\P{Han}]+`) fmt.Printf("%q\n", reg.FindAllString(s2, -1)) }d:\goprojects\src\day1\正则\exp2>go run main.go
[$ # % * & @ @ ^ & % ! $]
[Z 2 2 W h k C 9 K N w R I b B i t p Q Y U F J q d d Q V z 5 I r 7 j C L q z 3 F O C 2 x 5 C D 6 Z z v h N d k 5 V v 1 Q U D H b S c n B s 4 9 4 n g F H 0 B J 6 G x t X g 2 T]
[h k w b i t p q d d z r j q z x z v h d k v b c n s n g x t g]
[Z W C K N R I B Q Y U F J Q V I C L F O C C D Z N V Q U D H S B F H B J G X T]
[2 2 9 5 7 3 2 5 6 5 1 4 9 4 0 6 2]
[河 北 省 河 南 省 湖 北 省 湖 南 省]
["河北省河南省" "湖北省" "湖南省"]
["hbshns" "hbshns"]
d:\goprojects\src\day1\正则\exp2>