golang正则使用记录

先介绍几种常用的方法:

1、使用MatchString函数或Match函数

func MatchString(pattern string, s string) (bool, error) {}
func Match(pattern string, b []byte) (bool, error) {}

regexp.MatchString(pattern string, s string) pattern为正则表达式,s为需要校验的字符串
regexp.Match(pattern string, b []byte) pattern为正则表达式,s为需要校验的字符串
它们的作用都是匹配,区别在于参数为字符串和切片

实例如下:

match,_:=regexp.MatchString("p([a-z]+)ch","peddach")  //返回的第一个参数是bool类型即匹配结果,第二个参数是error类型
fmt.Println(match)  //结果为true

2、使用 Compile函数或MustCompile函数

它们的区别是Compile返回两个参数Regexp,error类型,而MustCompile只返回Regexp类型

func Compile(expr string) (*Regexp, error) {}
func MustCompile(str string) *Regexp {}

它们的作用是将正则表达式进行编译,返回优化的 Regexp 结构体,该结构体有许多方法。

实例如下:

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、Find查找

查找正则匹配字串
(注:函数名包含string的所传参数为string 其他的均为[]byte 带All是所有)

func (re *Regexp) Find(b []byte) []byte
func (re *Regexp) FindString(s string) string
func (re *Regexp) FindAll(b []byte, n int) [][]byte
func (re *Regexp) FindAllString(s string, n int) []string

查找正则匹配的字符串位置(注:函数名包含string的所传参数为string 其他的均为[]byte 带All是所有)

func (re *Regexp) FindIndex(b []byte) (loc []int)
func (re *Regexp) FindStringIndex(s string) (loc []int)
func (re *Regexp) FindAllIndex(b []byte, n int) [][]int
func (re *Regexp) FindAllStringIndex(s string, n int) [][]int

4、替换

正则替换

func (re *Regexp) ReplaceAll(src []byte, repl []byte) []byte 
func (re *Regexp) ReplaceAllString(src string, repl string) string

按原文替换

func (re *Regexp) ReplaceAllLiteral(src []byte, repl []byte) []byte
func (re *Regexp) ReplaceAllLiteralString(src string, repl string) string

函数处理替换源字串

func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string

5、Regexp结构体中一些常用的方法

r,_:=regexp.Compile("p([a-z]+)ch")
fmt.Println(r.MatchString("peach"))  //结果: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

转载:
作者:juggleczg
链接:https://www.jianshu.com/p/04de96b6ae40
来源:简书

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值