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

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

内容简介: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

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

查看所有标签

猜你喜欢:

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

创业的艺术2.0

创业的艺术2.0

〔美〕盖伊·川崎 / 刘悦、段歆玥 / 译言·东西文库/电子工业出版社 / 2016-9 / 68

“创业者导师”——盖伊•川崎的《创业的艺术2.0》被阿丽亚娜•赫芬顿评为“终极的创业手册”。无论您是企业家、小企业主、企业开拓者还是非盈利组织的领导人,都可以让你的产品、服务或理念获得成功。 盖伊选取了不用角度,探索前十年商界的巨大变化,并寻求解决之道。曾经所向披靡的市场巨头深陷水深火热之中,社交媒体也取代了人际关系和广告,成为营销推广的主要渠道。众筹也成为广大投资者的可行之举。“云”更是每......一起来看看 《创业的艺术2.0》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具