01-判断正则匹配、判断正则合法性

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

使用示例(判断字串完全英文)

func CheckStringWhetherEn(myString string)bool{
b,err :=regexp.MatchString("^([A-z]+)$",myString)
if err != nil {
fmt.Println(err)
}
return b
}

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

posted on   运维开发玄德公  阅读(35)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示