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就可用于匹配文本。
- 在匹配文本时,该正则表达式会尽可能早的开始匹配,并且在匹配过程中选择回溯搜索到的第一个匹配结果,这种模式被称为“leftmost-first”。
- Perl、Python和其他实现都采用了这种模式,但本包的实现没有回溯的损耗。对POSIX的“leftmost-longest”模式,参见CompilePOSIX。
func CompilePOSIX
func CompilePOSIX(expr string) (*Regexp, error)
- 类似Compile但会将语法约束到POSIX ERE(egrep)语法,并将匹配模式设置为leftmost-longest。
- 在匹配文本时,该正则表达式会尽可能早的开始匹配,并且在匹配过程中选择搜索到的最长的匹配结果,这种模式被称为“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 有什么区别
- Perl和POSIX兼容的正则表达式在很大程度上相似,但在一些关键方面有所不同,例如子匹配。
- 假设你有一个正则表达式(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