go语言中正则模块的使用详解

栏目: Go · 发布时间: 6年前

内容简介:5. func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte{}

GO语言中正则模块的使用详解

前景摘要:

* 在 go 语言中正则的包是regexp
* 在使用的时候,首先应该导入此包

1. func Compile(expr string) (*Regexp, error){}

用于解析正则表达式并返回(如果成功)可用于匹配文本的Regexp对象。同时会返回一个error
str := "adsd123"
re, _ := regexp.Compile("[a-b]{1}")
res := re.MatchString(str)   # 返回值类型为布尔值
fmt.Println(res)
输出:
true

2. func CompilePOSIX(expr string) (*Regexp, error){}

用于解析正则表达式并返回(如果成功)可用于匹配文本的Regexp对象。同时会返回一个error(类似于Compile)
str := "adsd123"
re, _ := regexp.CompilePOSIX("[a-b]{1}")
res := re.MatchString(str)
fmt.Println(res)
输出:
true

3. func MustCompile(str string) *Regexp{}

如果无法解析表达式,MustCompile就像编译一样恐慌。它简化了保存已编译正则表达式的全局变量的安全初始化。

4. func (re Regexp) Copy() Regexp{}

Copy返回从re复制的新Regexp对象。在一个副本上调用最长不会影响另一个副本。(不推荐使用)

5. func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte{}

//MatchString返回的第一个参数是bool类型即匹配结果,第二个参数是error类型
sourceStr := `my email is gerrylon@163.com`
matched, _ := regexp.MatchString(`[\w-]+@[\w]+(?:\.[\w]+)+`, sourceStr)
fmt.Printf("%v", matched) // true

r,_:=regexp.Compile("p([a-z]+)ch")
b:=r.MatchString("peach")
fmt.Println(b)  //结果为true

//Compile函数或MustCompile函数是将正则表达式进行编译,返回优化的 Regexp 结构体
//区别是Compile返回两个参数*Regexp,error类型,而MustCompile只返回*Regexp类型
r1:=regexp.MustCompile("p([a-z]+)ch")
b1:=r1.MatchString("peach")
fmt.Println(b)   //结果为true

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]

var sourceStr string = `
test text     lljflsdfjdskal
gerrylon@163.com
abc@gmail.com
someone@sina.com.cn`

re := regexp.MustCompile(`[\w-]+@([\w]+(?:\.[\w]+)+)`)
matched := re.FindAllStringSubmatch(sourceStr, -1)
for _, match := range matched {
    fmt.Printf("email is: %s, domain is: %s\n", match[0], match[1])
}
//email is: gerrylon@163.com, domain is: 163.com 
//email is: abc@gmail.com, domain is: gmail.com 
//email is: someone@sina.com.cn, domain is: sina.com.cn

//返回完全匹配和局部匹配的索引位置
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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Mining

Data Mining

Jiawei Han、Micheline Kamber、Jian Pei / Morgan Kaufmann / 2011-7-6 / USD 74.95

The increasing volume of data in modern business and science calls for more complex and sophisticated tools. Although advances in data mining technology have made extensive data collection much easier......一起来看看 《Data Mining》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具