標籤:GO Regex
[TOC]
Go語言Regex方式一:使用Compile
package mainimport ( "fmt" "regexp")const text = "my email is [email protected]"func main() { //re 是 Regex的匹配器 re, err := regexp.Compile("[email protected]") if err != nil { panic(err) } result := re.FindString(text) fmt.Println("result:\t", result)}
運行結果:
result: [email protected]Process finished with exit code 0
==此方法式,存在的問題?==
Compile方法中的Regex,Go語言不知道是否正確,有可能使用者寫的Regex是錯誤的。
方式二:使用MustCompile方法
==好處就是,參數必須是正確的Regex==
例子1
package mainimport ( "fmt" "regexp")const text_1 = "my email is [email protected]"func main() { //目前的Regex,僅僅是匹配一個值,[email protected] re := regexp.MustCompile("[email protected]") match := re.FindString(text_1) fmt.Println(match)}
運行結果:
[email protected]Process finished with exit code 0
==問題 .+ 與 .*的區別==
. 表示可以匹配任何字元 .+ 表示可以匹配1以上的字元,也就是說,只少有一個字元 .* 表示可以匹配0個以上的字元,也就是說,0個以上字元 其實,+,* 都是匹配的數量
例子2
package mainimport ( "fmt" "regexp")const text_1 = "my email is [email protected]"func main() { //目前的Regex,僅僅是匹配一個值,[email protected] re := regexp.MustCompile("[email protected]") match := re.FindString(text_1) fmt.Println(match)}
運行結果:
[email protected]Process finished with exit code 0
==如何匹配Regex中一個點呢?==
如在點的前面,添加一個反斜線\, 但是,Go語言會將反斜線當做是逸出字元,因此,需要添加兩個反斜線 \\. 同時,Go 語言,可以不使用"", 也可以使用反單引號,`` 來引用Regex,這樣的話,就不需要反斜線了,
例子3
package mainimport ( "fmt" "regexp")const text_3 = "my email is [email protected]"func main() { //目前的Regex,僅僅是匹配一個值,[email protected] re := regexp.MustCompile(`[email protected]+\..+`) match := re.FindString(text_3) fmt.Println(match)}
運行結果:
my email is [email protected]Process finished with exit code 0
==存在問題?==
將這條語句全部列印出來,而不是僅僅符合要求的哪些欄位
例子4
package mainimport ( "fmt" "regexp")const text_4 = "my email is [email protected]"func main() { //只匹配小寫字母,大寫字母,數字,不允許有特殊符號 re := regexp.MustCompile(`[a-zA-Z0-9][email protected][a-zA-Z0-9]+\.[a-zA-Z0-9]+`) match := re.FindString(text_4) fmt.Println(match)}
運行結果:
[email protected]Process finished with exit code 0
例子5 匹配多個時,如何處理?
package mainimport ( "fmt" "regexp")const text_5 = ` my email is [email protected] my email is [email protected] my email is [email protected] my email is [email protected] my email is [email protected]`func main() { //在[]裡, . 不需要 添加 逸出字元 re := regexp.MustCompile(`[a-zA-Z0-9][email protected][a-zA-Z0-9]+\.[a-zA-Z0-9]+`) //-1 表示,要匹配所有滿足條件的詞 match := re.FindAllString(text_5, -1) fmt.Println(match)}
運行結果:
[[email protected] [email protected] [email protected] [email protected] [email protected]]Process finished with exit code 0
例子6,如何提取出 名字,網域名稱呢?
==Regex具有提取功能,只需要將要提取的字元,用小括弧 括起來就可以了==
package mainimport ( "fmt" "regexp")const text_6 = ` my email is [email protected] my email is [email protected] my email is [email protected] my email is [email protected] my email is [email protected]`func main() { //在[]裡, . 不需要 添加 逸出字元 re := regexp.MustCompile(`([a-zA-Z0-9]+)@([a-zA-Z0-9]+)(\.[a-zA-Z0-9.]+)`) //-1 表示,要匹配所有滿足條件的詞 match := re.FindAllStringSubmatch(text_6, -1) for _, value := range match { fmt.Println(value) }}
運行結果:
[[email protected] k8sAndDocker google .com][[email protected] spark qq .com][[email protected] hadoop 126 .com][[email protected] kafka 163 .com][[email protected] docker 163docker .com.cn]Process finished with exit code 0
Go語言之Regex