Regexp package is needed to use regular check in Go language
First introduce several commonly used methods:
1. Use the MatchString function
regexp.MatchString(pattern string, s string) pattern is a regular expression, s is the character to be checked
Examples are as follows:
match,_:=regexp.MatchString("p([a-z]+)ch","peddach") 返回的第一个参数是bool类型即匹配结果,第二个参数是error类型
fmt.Println(match) //结果为true
2. Use Compile function or MustCompile function
. The difference is that Compile returns two parameters *Regexp and error type, while MustCompile only returns *Regexp type
func Compile(expr string) (*Regexp, error) {}
func MustCompile(str string) *Regexp {}
Their function is to compile the regular expression and return an optimized Regexp structure, which requires many methods.
Examples are as follows:
r,_:=regexp.Compile("p([a-z]+)ch")
b:=r.MatchString("peach")
fmt.Println(b) //结果为true
r1:=regexp.MustCompile("p([a-z]+)ch")
b1:r1.MatchString("peach")
fmt.Println(b) //结果为true
3. Some commonly used methods in Regexp structure
r,_:=regexp.Compile("p([a-z]+)ch")
fmt.Println(r.MatchString("peach")) //j结果:true
//查找匹配的字符串
fmt.Println(r.FindString("peach punch")) //打印结果:peach
//查找匹配字符串开始和结束位置的索引,而不是匹配内容[0 5]
fmt.Println(r.FindStringIndex("peach punch")) //打印结果: [0 5]
//返回完全匹配和局部匹配的字符串,例如,这里会返回 p([a-z]+)ch 和 `([a-z]+) 的信息
fmt.Println(r.FindStringSubmatch("peach punch")) //打印结果:[peach ea]
//返回完全匹配和局部匹配的索引位置
fmt.Println(r.FindStringSubmatchIndex("peach punch")) //打印结果: [0 5 1 3]
//返回所有的匹配项,而不仅仅是首次匹配项。正整数用来限制匹配次数
fmt.Println(r.FindAllString("peach punch pinch",-1)) //打印结果:[peach punch pinch]
fmt.Println(r.FindAllString("peach punch pinch",2)) //匹配两次 打印结果:[peach punch]
//返回所有的完全匹配和局部匹配的索引位置
fmt.Println(r.FindAllStringSubmatchIndex("peach punch pinch",-1))
//打印结果: [[0 5 1 3] [6 11 7 9] [12 17 13 15]]
//上面的例子中,我们使用了字符串作为参数,并使用了如 MatchString 这样的方法。我们也可以提供 []byte参数并将 String 从函数命中去掉。
fmt.Println(r.Match([]byte("peach"))) //打印结果:true
r1:=regexp.MustCompile("p([a-z]+)ch")
//将匹配的结果,替换成新输入的结果
fmt.Println(r1.ReplaceAllString("a peach","<fruit>")) //打印结果: a <fruit>
//Func 变量允许传递匹配内容到一个给定的函数中,
in:=[]byte("a peach")
out:=r1.ReplaceAllFunc(in,bytes.ToUpper)
fmt.Printf(string(out)) //打印结果: a PEACH