1. 程式人生 > >go語言與正則表示式

go語言與正則表示式

go語言正則表示式(匹配中文/匹配漢字)

regexp.Compile函式的用法
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.MustCompile函式的用法

該函式比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中國[email protected]#&*+_¥œø]+$")
	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
78h+¥œ 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會衝突,所以最好統一寫在英文雙引號裡,就不會報錯。