go regexp正则匹配

正则匹配
(1)regexp.Match
    ok, _ := regexp.Match(pat, []byte(searchIn))
(2)regexp.MatchString

(3)compile + find/replace
先compile()和mustcompile()函数来检索正则表达式
然后用MatchXX或者FindXX或者ReplaceXX函数来匹配或查找或替换字符串

匹配
regexp.Compile(pat).Match
regexp.Compile(pat).MatchString
查找:
regexp.Compile(pat).FindString
regexp.Compile(pat).FindAllString
regexp.Compile(pat).FindStringIndex
regexp.Compile(pat).FindAllStringIndex
regexp.Compile(pat).FindStringSubmatch
regexp.Compile(pat).FindAllStringSubmatch
regexp.Compile(pat).FindStringSubmatchIndex
regexp.Compile(pat).FindAllStringSubmatchIndex
替换:
regexp.Compile(pat).ReplaceAll()
regexp.Compile(pat).ReplaceAllString(searchIn, 替换内容)
regexp.Compile(pat).ReplaceAllStringFunc(searchIn, 替换函数)

语法格式
//(1)Compile与MustCompile解析正则表达式expr是否合法,合法则返回Regexp指针对象
//    异:当正则表达式不合法时,Compile仅返回error值,MustCompile会抛出异常
//(2)FindAllStringXX等函数的n,n=-1匹配所有,n!=-1只匹配n次
//正则
func Compile(expr string) (*Regexp, error)
func MustCompile(expr string) *Regexp
//匹配
func (re *Regexp) Match(b []byte) bool
func (re *Regexp) MatchString(s string) bool
//查找
func (re *Regexp) FindString(s string) string
func (re *Regexp) FindAllString(s string, n int) []string
func (re *Regexp) FindStringIndex(s string) (loc []int)
func (re *Regexp) FindAllStringIndex(s string, n int) [][]int
func (re *Regexp) FindStringSubmatch(s string) []string
func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
func (re *Regexp) FindStringSubmatchIndex(s string) []int
func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
//替换
func (re *Regexp) ReplaceAll(src []byte, repl []byte) []byte
func (re *Regexp) ReplaceAllString(src string, repl string) string

详见:https://www.cainiaojc.com/golang/go-regex.html​​​​​​​

//func Compile(expr string) (*Regexp, error)
r, _ := regexp.Compile(`f([a-z]+)`)
 
//func (re *Regexp) Match(b []byte) bool
fmt.Println(r.Match([]byte("foo"))) //true
 
//func (re *Regexp) MatchString(s string) bool
fmt.Println(r.MatchString("foo")) //true
 
//func (re *Regexp) FindString(s string) string
//只匹配一次
fmt.Println(r.FindString("foo func")) //foo
 
//func (re *Regexp) FindStringIndex(s string) (loc []int)
fmt.Println(r.FindStringIndex("demo foo func")) //[5 8]
 
//func (re *Regexp) FindStringSubmatch(s string) []string
//只匹配一次,返回的结果中,索引为0的值是整个匹配串的值,第二个值是子表达式的值
fmt.Println(r.FindStringSubmatch("this foo func fan")) //[foo oo]
 
//对于FindStringSubmatch,如果表达式中没有子表达式,则不检测子表达式
demo, _ := regexp.Compile(`foo`)
fmt.Println(demo.FindStringSubmatch("foo")) //[foo]
 
//func (re *Regexp) FindStringSubmatchIndex(s string) []int
fmt.Println(r.FindStringSubmatchIndex("foo func")) //[0 3 1 3]
 
//func (re *Regexp) FindAllString(s string, n int) []string
//n为-1时,匹配所有符合条件的字符串,n不为-1时,表示只匹配n次
fmt.Println(r.FindAllString("foo func fan", -1)) //[foo func fan]
fmt.Println(r.FindAllString("foo func fan", 2))  //[foo func]
 
//func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int
//n同样是表示匹配的次数,-1表示匹配所有
fmt.Println(r.FindAllStringSubmatchIndex("foo func demo fan", -1))
//[[0 3 1 3] [4 8 5 8] [14 17 15 17]]
 
//替换
 
//func (re *Regexp) ReplaceAll(src []byte, repl []byte) []byte
fmt.Println(string(r.ReplaceAll([]byte("this is foo, that is func, they are fan"), []byte("x"))))
//this is x, that is x, they are x
 
//func (re *Regexp) ReplaceAllString(src string, repl string) string
fmt.Println(r.ReplaceAllString("this is foo, that is func, they are fan", "xx"))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值