[TOC]
Go language Regular expression method one: use Compile
package mainimport ( "fmt" "regexp")const text = "my email is [email protected]"func main() { //re 是 正则表达式的匹配器 re, err := regexp.Compile("[email protected]") if err != nil { panic(err) } result := re.FindString(text) fmt.Println("result:\t", result)}
Operation Result:
result: [email protected]Process finished with exit code 0
= = This method, existing problems? ==
Compile the regular expression in the method, the go language does not know whether it is correct, it is possible that the user writes the regular expression is wrong.
Mode two: Using the Mustcompile method
= = The advantage is that the parameter must be the correct regular expression = =
Example 1
package mainimport ( "fmt" "regexp")const text_1 = "my email is [email protected]"func main() { //目前的正则表达式,仅仅是匹配一个值,[email protected] re := regexp.MustCompile("[email protected]") match := re.FindString(text_1) fmt.Println(match)}
Operation Result:
[email protected]Process finished with exit code 0
= = question-the difference between + and. * =
. 表示可以匹配任何字符 .+ 表示可以匹配1以上的字符,也就是说,只少有一个字符 .* 表示可以匹配0个以上的字符,也就是说,0个以上字符 其实,+,* 都是匹配的数量
Example 2
package mainimport ( "fmt" "regexp")const text_1 = "my email is [email protected]"func main() { //目前的正则表达式,仅仅是匹配一个值,[email protected] re := regexp.MustCompile("[email protected]") match := re.FindString(text_1) fmt.Println(match)}
Operation Result:
[email protected]Process finished with exit code 0
= = How to match a point in a regular expression? ==
如在点的前面,添加一个反斜杠\, 但是,Go语言会将反斜杠当做是转义字符,因此,需要添加两个反斜杠 \\. 同时,Go 语言,可以不使用"", 也可以使用反单引号,`` 来引用正则表达式,这样的话,就不需要反斜杠了,
Example 3
package mainimport ( "fmt" "regexp")const text_3 = "my email is [email protected]"func main() { //目前的正则表达式,仅仅是匹配一个值,[email protected] re := regexp.MustCompile(`[email protected]+\..+`) match := re.FindString(text_3) fmt.Println(match)}
Operation Result:
my email is [email protected]Process finished with exit code 0
= Is there a problem? ==
Print out all of this statement, not just what fields are required
Example 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)}
Operation Result:
[email protected]Process finished with exit code 0
Example 5 when matching multiple, how to deal with?
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)}
Operation Result:
[[email protected] [email protected] [email protected] [email protected] [email protected]]Process finished with exit code 0
Example 6, how to extract the name, domain name?
= = The regular expression has the function of extracting, only the characters that will be extracted, enclosed in parentheses.
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) }}
Operation Result:
[[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
The regular expression of the Go language