[TOC]
Go语言正则表达式
方式一:使用Compile
package main
import (
"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)
}
运行结果:
result: [email protected]
Process finished with exit code 0
==此方法式,存在的问题?==
Compile方法中的正则表达式,Go语言不知道是否正确,有可能用户写的正则表达式是错误的。
方式二:使用MustCompile方法
==好处就是,参数必须是正确的正则表达式==
例子1
package main
import (
"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)
}
运行结果:
[email protected]
Process finished with exit code 0
==问题 .+ 与 .*的区别==
. 表示可以匹配任何字符
.+ 表示可以匹配1以上的字符,也就是说,只少有一个字符
.* 表示可以匹配0个以上的字符,也就是说,0个以上字符
其实,+,* 都是匹配的数量
例子2
package main
import (
"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)
}
运行结果:
[email protected]
Process finished with exit code 0
==如何匹配正则表达式中一个点呢?==
如在点的前面,添加一个反斜杠\,
但是,Go语言会将反斜杠当做是转义字符,因此,需要添加两个反斜杠 \\.
同时,Go 语言,可以不使用"", 也可以使用反单引号,`` 来引用正则表达式,这样的话,就不需要反斜杠了,
例子3
package main
import (
"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)
}
运行结果:
my email is [email protected]
Process finished with exit code 0
==存在问题?==
将这条语句全部打印出来,而不是仅仅符合要求的哪些字段
例子4
package main
import (
"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 main
import (
"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,如何提取出 名字,域名呢?
==正则表达式具有提取功能,只需要将要提取的字符,用小括号 括起来就可以了==
package main
import (
"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
原文地址:http://blog.51cto.com/xingej/2113546
时间: 2024-11-07 08:56:45