golang正则regexp包使用-01-判断正则匹配、判断正则合法性(Match,MatchString,MatchReader,Compile,CompilePOSIX,MustCompile)

1. Match 系列函数(判断是否匹配)

函数 匹配字串类型 返回值 返回类型
Match() [ ]byte 判断是否匹配 bool
MatchString() String 判断是否匹配 bool
MatchReader() io.RuneReader 判断是否匹配 bool

1.1 Match()

语法

func Match(pattern string, b []byte) (matched bool, err error)

完整示例

  • 代码
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
    
	pattern := "关羽.*"
	myString := "关羽关羽关云长"
	b,_ := (regexp.Match(pattern, []byte(myString)))
	fmt.Printf("查找结果:%t",b)
}
  • 结果
查找结果:true

1.2 MatchString()

语法

func MatchString(pattern string, s string) (matched bool, err error)

完整示例

  • 代码
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
    
	pattern := "玄德.*"
	myString := "刘备刘玄德"
	b,_ := regexp.MatchString(pattern, myString)
	fmt.Printf("查找结果:%t",b)
}
  • 结果
查找结果:true

1.3 MatchReader()

语法

func MatchReader(pattern string, r io.RuneReader) (matched bool, err error)

完整示例

package main

import (
	"bytes"
	"fmt"
	"regexp"
)

func main() {
    
    
	r := bytes.NewReader([]byte("关羽关云长"))
	b,_ := regexp.MatchReader("云长.*", r)
	fmt.Printf("查找结果:%t",b)
}
  • 结果
查找结果:true

2. Compile系列函数 (判断正则合法性)

函数 匹配字串类型 返回值 返回类型
Compile() —— 正则,是否合法 (*Regexp, error)
CompilePOSIX() —— 正则,是否合法 (*Regexp, error)
MustCompile() —— 正则 *Regexp
MustCompilePOSIX() —— 正则 *Regexp

差别:

  • POSIX 支持POSIX正则表达式
  • Must 不返回err,但是抛出异常

2.1 Compile()

语法

func Compile(expr string) (*Regexp, error)

完整示例

  • 代码
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
    
	reg,_ := regexp.Compile(`玄德.*`)
	fmt.Printf("返回正则表达式字串为:%+v",reg)
}
  • 结果
返回正则表达式字串为:玄德.*

2.2 CompilePOSIX()

语法

func CompilePOSIX(expr string) (*Regexp, error)

完整示例

  • 代码
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
    

	reg,_ := regexp.CompilePOSIX(`.*\.`)
	fmt.Printf("返回正则表达式字串为:%q\n", reg)
}
  • 结果
返回正则表达式字串为: ".*\\."

2.3 MustCompile()

语法

func MustCompile(str string) *Regexp

完整示例

  • 代码
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
    
	reg := regexp.MustCompile("刘备.*")
	fmt.Printf("返回正则表达式字串为:%q\n", reg)
}

  • 示例
返回正则表达式字串为:"刘备.*"

2.4 MustCompilePOSIX()

语法

func MustCompile(str string) *Regexp

3. Match系列方法

函数 匹配字串类型 返回值 返回类型
Match() [ ]byte 判断是否匹配 bool
MatchString() String 判断是否匹配 bool
MatchReader() io.RuneReader 判断是否匹配 bool

和Match函数比,Match方法不需要再传入正则,正则在实例化结构体时已经传入了结构体。比如:

reg := regexp.MustCompile(pattern)

或者

reg,err := regexp.Compile(pattern)

3.1 Must()

语法

func (re *Regexp) Match(b []byte) bool

完整示例

  • 代码
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
    
	pattern := "刘.*"
	myString := "刘备刘玄德"

	reg := regexp.MustCompile(pattern)
	b := reg.Match([]byte(myString))
	fmt.Printf("查找结果:%t",b)
}
  • 显示
查找结果:true

3.2 MustString()

语法

func (re *Regexp) Match(b []byte) bool

完整示例

  • 代码
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
    
	pattern := "刘.*"
	myString := "刘备刘玄德"

	reg := regexp.MustCompile(pattern)
	b := reg.MatchString([]byte(myString))
	fmt.Printf("查找结果:%t",b)
}
  • 显示
查找结果:true

3.3 MustReader()

语法

func (re *Regexp) Match(b []byte) bool

完整示例

  • 代码
package main

import (
	"bytes"
	"fmt"
	"regexp"
)

func main() {
    
    
	pattern := "刘.*"
	myString := "刘备刘玄德"
	r := bytes.NewReader([]byte(myString))

	reg := regexp.MustCompile(pattern)
	b := reg.MatchReader(r)
	fmt.Printf("查找结果:%t",b)
}
  • 显示
查找结果:true

猜你喜欢

转载自blog.csdn.net/xingzuo_1840/article/details/125351135