go 正则函数

func Match

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

Match检查b中是否存在匹配pattern的子序列。

func MatchString

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

func Compile

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

Compile解析并返回一个正则表达式。如果成功返回,该Regexp就可用于匹配文本。

  1. 在匹配文本时,该正则表达式会尽可能早的开始匹配,并且在匹配过程中选择回溯搜索到的第一个匹配结果,这种模式被称为“leftmost-first”。
  2. Perl、Python和其他实现都采用了这种模式,但本包的实现没有回溯的损耗。对POSIX的“leftmost-longest”模式,参见CompilePOSIX。

func CompilePOSIX

func CompilePOSIX(expr string) (*Regexp, error)
  1. 类似Compile但会将语法约束到POSIX ERE(egrep)语法,并将匹配模式设置为leftmost-longest。
  2. 在匹配文本时,该正则表达式会尽可能早的开始匹配,并且在匹配过程中选择搜索到的最长的匹配结果,这种模式被称为“leftmost-longest”

func MustCompile

func MustCompile(str string) *Regexp

MustCompile类似Compile但会在解析失败时panic,主要用于全局正则表达式变量的安全初始化。

func MustCompilePOSIX

func MustCompilePOSIX(str string) *Regexp

MustCompilePOSIX类似CompilePOSIX但会在解析失败时panic,主要用于全局正则表达式变量的安全初始化。

regexp.Compile 和 regexp.CompilePOSIX 有什么区别

  1. Perl和POSIX兼容的正则表达式在很大程度上相似,但在一些关键方面有所不同,例如子匹配。
  2. 假设你有一个正则表达式(foo | foobar)。当将这个表达式与匹配多个子表达式的字符串进行匹配时(例如,foobarbaz将同时匹配子模式foo和foobar),与Perl兼容的正则表达式将返回第一个匹配(foo),而与POSIX兼容的正则表达式将返回最长的匹配(foobar)。
func main() {
    pattern := "(foo|foobar)"
    str := []byte("foobarbaz")

    rPCRE, _ := regexp.Compile(pattern)
    rPOSIX, _ := regexp.CompilePOSIX(pattern)

    matchesPCRE := rPCRE.Find(str)
    fmt.Println(string(matchesPCRE))
    // prints "foo"

    matchesPOSIX := rPOSIX.Find(str)
    fmt.Println(string(matchesPOSIX))
    // prints "foobar"
}

func (re *Regexp) FindAllStringSubmatch

func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string

 根据解析好的规则(结构体形式),从指定字符串中提取需要的信息

注意,要使用前面regexp.MustCompile() 函数调用的返回值,来调用此函数

func (re *Regexp) FindStringSubmatch

func (re *Regexp) FindStringSubmatch(s string)[]string

用法和 FindAllStringSubmatch 差不多,示例:

提取字符串中的部分,包含括号和不包含括号的用法:

package main

import (
	"fmt"
	"regexp"
)

// 特例:找出中英⽂"()()"括号中间的字符,例如:华南地区(⼴州) -> ⼴州
func findDestStr(src string) string {
	compileRegex := regexp.MustCompile("((.*?))") // 中⽂括号,例如:华南地区(⼴州) -> ⼴州
	matchArr := compileRegex.FindStringSubmatch(src)
	if len(matchArr) == 0 {
		compileRegex := regexp.MustCompile("\\((.*?)\\)") // 兼容英⽂括号并取消括号的转义,例如:华南地区 (⼴州) -> ⼴州。
		matchArr = compileRegex.FindStringSubmatch(src)
	}
	// fmt.Println("提取字符串内容:", matchArr[len(matchArr)-1])
	if len(matchArr) > 0 {
		return matchArr[len(matchArr)-1]
	}
	return ""
}

func main() {
	str := "aaaa"
	compileRegex := regexp.MustCompile(".*/(.*?)/inputs/.*") // 正则表达式的分组,以括号()表⽰,每⼀对括号就是我们匹配到的⼀个⽂本,可以把他们提取出来。
	matchArr := compileRegex.FindStringSubmatch(str)   // FindStringSubmatch ⽅法是提取出匹配的字符串,然后通过[]string返回。我们可以看到,第1个匹配到的是这
	fmt.Println("提取字符串内容:", matchArr[len(matchArr)-1]) // 输出:蜜桃乌龙茶
	return
}

func (*Regexp) String

func (re *Regexp) String() string

String返回用于编译成正则表达式的字符串。 

func (*Regexp) Match

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

Match检查b中是否存在匹配pattern的子序列。

func (*Regexp) MatchString

func (re *Regexp) MatchString(s string) bool

MatchString类似Match,但匹配对象是字符串。

func (*Regexp) Find

func (re *Regexp) Find(b []byte) []byte

Find返回保管正则表达式re在b中的最左侧的一个匹配结果的[]byte切片。如果没有匹配到,会返回nil。

func (*Regexp) FindString

func (re *Regexp) FindString(s string) string

Find返回保管正则表达式re在b中的最左侧的一个匹配结果的字符串。如果没有匹配到,会返回"“;但如果正则表达式成功匹配了一个空字符串,也会返回”"。如果需要区分这种情况,请使用FindStringIndex 或FindStringSubmatch。

…其他方法请查询:https://studygolang.com/pkgdochttps://studygolang.com/pkgdoc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴国进

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值