package regexp

import "regexp"

Package regexp 实现了正则表达式搜索。

所接受的正则表达式语法与 Perl、Python 和其他语言使用的通用语法相同。 更准确地说,它是 RE2 所接受并在 https://golang.org/s/re2syntax 中描述的语法, 但不包括 \C。 关于语法概览,请参阅 regexp/syntax 包。

本包提供的正则表达式实现保证在输入大小的线性时间内运行。 (这是大多数开源正则表达式实现所不保证的属性。) 关于此属性的更多信息,请参阅 https://swtch.com/~rsc/regexp/regexp1.html 或任何关于自动机理论的书籍。

所有字符都是 UTF-8 编码的码点。 遵循 utf8.DecodeRune 的规则,无效 UTF-8 序列中的每个字节 都被视为编码了 utf8.RuneError (U+FFFD)。

Regexp 有 16 个方法用于匹配正则表达式并标识匹配的文本。 它们的名称可由以下正则表达式匹配:

Find(All)?(String)?(Submatch)?(Index)?

如果存在 'All',则该函数匹配整个表达式的连续非重叠匹配。 紧邻前一个匹配的空匹配将被忽略。返回值是一个切片, 包含对应的非 'All' 函数的连续返回值。这些函数接受一个额外的整数参数 n。 如果 n >= 0,函数最多返回 n 个匹配/子匹配;否则,返回全部匹配。

如果存在 'String',则参数为字符串;否则为字节切片; 返回值会相应调整。

如果存在 'Submatch',则返回值是一个切片,标识表达式的连续子匹配。 子匹配是正则表达式中带括号的子表达式(也称为捕获组)的匹配, 按左括号的顺序从左到右编号。子匹配 0 是整个表达式的匹配, 子匹配 1 是第一个带括号子表达式的匹配,依此类推。

如果存在 'Index',匹配和子匹配通过输入字符串中的字节索引对来标识: result[2*n:2*n+2] 标识第 n 个子匹配的索引。n==0 的索引对标识整个表达式的匹配。 如果不存在 'Index',则匹配通过匹配/子匹配的文本来标识。 如果索引为负数或文本为 nil,则表示该子表达式未匹配输入中的任何字符串。 对于 'String' 版本,空字符串表示无匹配或匹配了空字符串。

还有一组方法可以应用于从 io.RuneReader 读取的文本: Regexp.MatchReaderRegexp.FindReaderIndexRegexp.FindReaderSubmatchIndex

此集合可能会增长。请注意,正则表达式匹配可能需要检查匹配返回的文本之外的文本, 因此从 io.RuneReader 匹配文本的方法在返回之前可能会读取输入中任意远的位置。

(还有一些其他方法不符合此模式。)

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	// Compile the expression once, usually at init time.
	// Use raw strings to avoid having to quote the backslashes.
	var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`)

	fmt.Println(validID.MatchString("adam[23]"))
	fmt.Println(validID.MatchString("eve[7]"))
	fmt.Println(validID.MatchString("Job[48]"))
	fmt.Println(validID.MatchString("snakey"))
}

Output:

true
true
false
false

Index

Examples

Functions

func Match

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

Match 报告字节切片 b 中是否包含正则表达式 pattern 的任何匹配。 更复杂的查询需要使用 Compile 和完整的 Regexp 接口。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	matched, err := regexp.Match(`foo.*`, []byte(`seafood`))
	fmt.Println(matched, err)
	matched, err = regexp.Match(`bar.*`, []byte(`seafood`))
	fmt.Println(matched, err)
	matched, err = regexp.Match(`a(b`, []byte(`seafood`))
	fmt.Println(matched, err)

}

Output:

true <nil>
false <nil>
false error parsing regexp: missing closing ): `a(b`

func MatchReader

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

MatchReader 报告 io.RuneReader 返回的文本中是否包含正则表达式 pattern 的任何匹配。 更复杂的查询需要使用 Compile 和完整的 Regexp 接口。

func MatchString

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

MatchString 报告字符串 s 中是否包含正则表达式 pattern 的任何匹配。 更复杂的查询需要使用 Compile 和完整的 Regexp 接口。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	matched, err := regexp.MatchString(`foo.*`, "seafood")
	fmt.Println(matched, err)
	matched, err = regexp.MatchString(`bar.*`, "seafood")
	fmt.Println(matched, err)
	matched, err = regexp.MatchString(`a(b`, "seafood")
	fmt.Println(matched, err)
}

Output:

true <nil>
false <nil>
false error parsing regexp: missing closing ): `a(b`

func QuoteMeta

func QuoteMeta(s string) string

QuoteMeta 返回一个字符串,其中转义了参数文本中所有正则表达式元字符; 返回的字符串是一个匹配该字面文本的正则表达式。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	fmt.Println(regexp.QuoteMeta(`Escaping symbols like: .+*?()|[]{}^$`))
}

Output:

Escaping symbols like: \.\+\*\?\(\)\|\[\]\{\}\^\$

Types

type Regexp

type Regexp struct {
	// contains filtered or unexported fields
}

Regexp 是已编译正则表达式的表示。 Regexp 可以安全地被多个 goroutine 并发使用, 但配置方法(如 Regexp.Longest)除外。

func Compile

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

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

在匹配文本时,该正则表达式返回在输入中尽可能早开始的匹配(最左匹配), 并在这些匹配中选择回溯搜索最先找到的那个。 这种所谓的最左优先匹配与 Perl、Python 和其他实现使用的语义相同, 尽管本包在实现时没有回溯的开销。 对于 POSIX 最左最长匹配,请参阅 CompilePOSIX

func CompilePOSIX

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

CompilePOSIX 类似于 Compile,但将正则表达式限制为 POSIX ERE (egrep) 语法,并将匹配语义改为最左最长匹配。

也就是说,在匹配文本时,该正则表达式返回在输入中尽可能早开始的匹配(最左匹配), 并在这些匹配中选择尽可能长的匹配。 这种所谓的最左最长匹配与早期正则表达式实现所使用的以及 POSIX 所规定的语义相同。

然而,可能存在多个最左最长匹配,且具有不同的子匹配选择, 在这一点上本包与 POSIX 存在差异。 在所有可能的最左最长匹配中,本包选择回溯搜索最先找到的那个, 而 POSIX 规定应选择使第一个子表达式长度最大化的匹配, 然后是第二个,依此类推从左到右。 POSIX 规则在计算上是不可行的,甚至定义也不完善。 详细信息请参阅 https://swtch.com/~rsc/regexp/regexp2.html#posix

func MustCompile

func MustCompile(str string) *Regexp

MustCompile 类似于 Compile,但在表达式无法解析时会 panic。 它简化了保存已编译正则表达式的全局变量的安全初始化。

func MustCompilePOSIX

func MustCompilePOSIX(str string) *Regexp

MustCompilePOSIX 类似于 CompilePOSIX,但在表达式无法解析时会 panic。 它简化了保存已编译正则表达式的全局变量的安全初始化。

func (*Regexp) AppendText

func (re *Regexp) AppendText(b []byte) ([]byte, error)

AppendText 实现了 encoding.TextAppender。输出与调用 Regexp.String 方法的结果相同。

注意在某些情况下输出是有损的:此方法不会标示 POSIX 正则表达式 (即通过调用 CompilePOSIX 编译的表达式), 也不会标示已调用 Regexp.Longest 方法的表达式。

func (*Regexp) Copy deprecated

func (re *Regexp) Copy() *Regexp

Copy 返回一个从 re 复制的新 Regexp 对象。 对一个副本调用 Regexp.Longest 不会影响另一个副本。

Deprecated: 在早期版本中,在多个 goroutine 中使用 Regexp 时, 为每个 goroutine 提供各自的副本有助于避免锁竞争。 从 Go 1.12 开始,不再需要使用 Copy 来避免锁竞争。 如果使用 Copy 的原因是创建两个具有不同 Regexp.Longest 设置的副本, 则 Copy 仍然是合适的。

func (*Regexp) Expand

func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte

Expand 将 template 追加到 dst 并返回结果;在追加过程中, Expand 用从 src 中提取的对应匹配替换模板中的变量。 match 切片应该是由 Regexp.FindSubmatchIndex 返回的。

在模板中,变量由 $name 或 ${name} 形式的子串表示, 其中 name 是由字母、数字和下划线组成的非空序列。 纯数字名称(如 $1)引用对应索引的子匹配; 其他名称引用使用 (?P<name>...) 语法命名的捕获括号。 对超出范围或未匹配的索引,或正则表达式中不存在的名称的引用, 将被替换为空切片。

在 $name 形式中,name 取尽可能长的匹配:$1x 等价于 ${1x} 而非 ${1}x,$10 等价于 ${10} 而非 ${1}0。

要在输出中插入字面 $ 符号,请在模板中使用 $$。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	content := []byte(`
	# comment line
	option1: value1
	option2: value2

	# another comment line
	option3: value3
`)

	// Regex pattern captures "key: value" pair from the content.
	pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)

	// Template to convert "key: value" to "key=value" by
	// referencing the values captured by the regex pattern.
	template := []byte("$key=$value\n")

	result := []byte{}

	// For each match of the regex in the content.
	for _, submatches := range pattern.FindAllSubmatchIndex(content, -1) {
		// Apply the captured submatches to the template and append the output
		// to the result.
		result = pattern.Expand(result, template, content, submatches)
	}
	fmt.Println(string(result))
}

Output:

option1=value1
option2=value2
option3=value3

func (*Regexp) ExpandString

func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte

ExpandString 类似于 Regexp.Expand,但模板和源是字符串。 它追加到并返回一个字节切片,以便让调用代码控制内存分配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	content := `
	# comment line
	option1: value1
	option2: value2

	# another comment line
	option3: value3
`

	// Regex pattern captures "key: value" pair from the content.
	pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)

	// Template to convert "key: value" to "key=value" by
	// referencing the values captured by the regex pattern.
	template := "$key=$value\n"

	result := []byte{}

	// For each match of the regex in the content.
	for _, submatches := range pattern.FindAllStringSubmatchIndex(content, -1) {
		// Apply the captured submatches to the template and append the output
		// to the result.
		result = pattern.ExpandString(result, template, content, submatches)
	}
	fmt.Println(string(result))
}

Output:

option1=value1
option2=value2
option3=value3

func (*Regexp) Find

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

Find 返回一个切片,持有正则表达式在 b 中最左匹配的文本。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`foo.?`)
	fmt.Printf("%q\n", re.Find([]byte(`seafood fool`)))

}

Output:

"food"

func (*Regexp) FindAll

func (re *Regexp) FindAll(b []byte, n int) [][]byte

FindAll 是 Regexp.Find 的 'All' 版本;它返回表达式所有连续匹配的切片, 如包注释中 'All' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`foo.?`)
	fmt.Printf("%q\n", re.FindAll([]byte(`seafood fool`), -1))

}

Output:

["food" "fool"]

func (*Regexp) FindAllIndex

func (re *Regexp) FindAllIndex(b []byte, n int) [][]int

FindAllIndex 是 Regexp.FindIndex 的 'All' 版本;它返回表达式所有连续匹配的切片, 如包注释中 'All' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	content := []byte("London")
	re := regexp.MustCompile(`o.`)
	fmt.Println(re.FindAllIndex(content, 1))
	fmt.Println(re.FindAllIndex(content, -1))
}

Output:

[[1 3]]
[[1 3] [4 6]]

func (*Regexp) FindAllString

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

FindAllString 是 Regexp.FindString 的 'All' 版本;它返回表达式所有连续匹配的切片, 如包注释中 'All' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a.`)
	fmt.Println(re.FindAllString("paranormal", -1))
	fmt.Println(re.FindAllString("paranormal", 2))
	fmt.Println(re.FindAllString("graal", -1))
	fmt.Println(re.FindAllString("none", -1))
}

Output:

[ar an al]
[ar an]
[aa]
[]

func (*Regexp) FindAllStringIndex

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

FindAllStringIndex 是 Regexp.FindStringIndex 的 'All' 版本; 它返回表达式所有连续匹配的切片,如包注释中 'All' 描述所定义。 返回值为 nil 表示无匹配。

func (*Regexp) FindAllStringSubmatch

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

FindAllStringSubmatch 是 Regexp.FindStringSubmatch 的 'All' 版本; 它返回表达式所有连续匹配的切片,如包注释中 'All' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(x*)b`)
	fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-", -1))
	fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-", -1))
	fmt.Printf("%q\n", re.FindAllStringSubmatch("-ab-axb-", -1))
	fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxb-ab-", -1))
}

Output:

[["ab" ""]]
[["axxb" "xx"]]
[["ab" ""] ["axb" "x"]]
[["axxb" "xx"] ["ab" ""]]

func (*Regexp) FindAllStringSubmatchIndex

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

FindAllStringSubmatchIndex 是 Regexp.FindStringSubmatchIndex 的 'All' 版本; 它返回表达式所有连续匹配的切片,如包注释中 'All' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(x*)b`)
	// Indices:
	//    01234567   012345678
	//    -ab-axb-   -axxb-ab-
	fmt.Println(re.FindAllStringSubmatchIndex("-ab-", -1))
	fmt.Println(re.FindAllStringSubmatchIndex("-axxb-", -1))
	fmt.Println(re.FindAllStringSubmatchIndex("-ab-axb-", -1))
	fmt.Println(re.FindAllStringSubmatchIndex("-axxb-ab-", -1))
	fmt.Println(re.FindAllStringSubmatchIndex("-foo-", -1))
}

Output:

[[1 3 2 2]]
[[1 5 2 4]]
[[1 3 2 2] [4 7 5 6]]
[[1 5 2 4] [6 8 7 7]]
[]

func (*Regexp) FindAllSubmatch

func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte

FindAllSubmatch 是 Regexp.FindSubmatch 的 'All' 版本;它返回表达式所有连续匹配的切片, 如包注释中 'All' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`foo(.?)`)
	fmt.Printf("%q\n", re.FindAllSubmatch([]byte(`seafood fool`), -1))

}

Output:

[["food" "d"] ["fool" "l"]]

func (*Regexp) FindAllSubmatchIndex

func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int

FindAllSubmatchIndex 是 Regexp.FindSubmatchIndex 的 'All' 版本; 它返回表达式所有连续匹配的切片,如包注释中 'All' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	content := []byte(`
	# comment line
	option1: value1
	option2: value2
`)
	// Regex pattern captures "key: value" pair from the content.
	pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)
	allIndexes := pattern.FindAllSubmatchIndex(content, -1)
	for _, loc := range allIndexes {
		fmt.Println(loc)
		fmt.Println(string(content[loc[0]:loc[1]]))
		fmt.Println(string(content[loc[2]:loc[3]]))
		fmt.Println(string(content[loc[4]:loc[5]]))
	}
}

Output:

[18 33 18 25 27 33]
option1: value1
option1
value1
[35 50 35 42 44 50]
option2: value2
option2
value2

func (*Regexp) FindIndex

func (re *Regexp) FindIndex(b []byte) (loc []int)

FindIndex 返回一个包含两个整数的切片,定义正则表达式在 b 中最左匹配的位置。 匹配本身位于 b[loc[0]:loc[1]]。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	content := []byte(`
	# comment line
	option1: value1
	option2: value2
`)
	// Regex pattern captures "key: value" pair from the content.
	pattern := regexp.MustCompile(`(?m)(?P<key>\w+):\s+(?P<value>\w+)$`)

	loc := pattern.FindIndex(content)
	fmt.Println(loc)
	fmt.Println(string(content[loc[0]:loc[1]]))
}

Output:

[18 33]
option1: value1

func (*Regexp) FindReaderIndex

func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int)

FindReaderIndex 返回一个包含两个整数的切片,定义正则表达式在从 io.RuneReader 读取的文本中最左匹配的位置。匹配文本在输入流中的字节偏移量为 loc[0] 到 loc[1]-1。 返回值为 nil 表示无匹配。

func (*Regexp) FindReaderSubmatchIndex

func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int

FindReaderSubmatchIndex 返回一个切片,持有标识正则表达式在由 io.RuneReader 读取的文本中最左匹配以及其子表达式的匹配(如果有)的索引对, 如包注释中 'Submatch' 和 'Index' 描述所定义。 返回值为 nil 表示无匹配。

func (*Regexp) FindString

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

FindString 返回一个字符串,持有正则表达式在 s 中最左匹配的文本。 如果没有匹配,返回值为空字符串, 但如果正则表达式成功匹配了空字符串,返回值也为空。 如果需要区分这些情况,请使用 Regexp.FindStringIndexRegexp.FindStringSubmatch

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`foo.?`)
	fmt.Printf("%q\n", re.FindString("seafood fool"))
	fmt.Printf("%q\n", re.FindString("meat"))
}

Output:

"food"
""

func (*Regexp) FindStringIndex

func (re *Regexp) FindStringIndex(s string) (loc []int)

FindStringIndex 返回一个包含两个整数的切片,定义正则表达式在 s 中最左匹配的位置。 匹配本身位于 s[loc[0]:loc[1]]。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`ab?`)
	fmt.Println(re.FindStringIndex("tablett"))
	fmt.Println(re.FindStringIndex("foo") == nil)
}

Output:

[1 3]
true

func (*Regexp) FindStringSubmatch

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

FindStringSubmatch 返回一个字符串切片,持有正则表达式在 s 中最左匹配的文本 以及其子表达式的匹配(如果有),如包注释中 'Submatch' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(x*)b(y|z)c`)
	fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-"))
	fmt.Printf("%q\n", re.FindStringSubmatch("-abzc-"))
}

Output:

["axxxbyc" "xxx" "y"]
["abzc" "" "z"]

func (*Regexp) FindStringSubmatchIndex

func (re *Regexp) FindStringSubmatchIndex(s string) []int

FindStringSubmatchIndex 返回一个切片,持有标识正则表达式在 s 中最左匹配 以及其子表达式的匹配(如果有)的索引对, 如包注释中 'Submatch' 和 'Index' 描述所定义。 返回值为 nil 表示无匹配。

func (*Regexp) FindSubmatch

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

FindSubmatch 返回一个切片的切片,持有正则表达式在 b 中最左匹配的文本 以及其子表达式的匹配(如果有),如包注释中 'Submatch' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`foo(.?)`)
	fmt.Printf("%q\n", re.FindSubmatch([]byte(`seafood fool`)))

}

Output:

["food" "d"]

func (*Regexp) FindSubmatchIndex

func (re *Regexp) FindSubmatchIndex(b []byte) []int

FindSubmatchIndex 返回一个切片,持有标识正则表达式在 b 中最左匹配 以及其子表达式的匹配(如果有)的索引对, 如包注释中 'Submatch' 和 'Index' 描述所定义。 返回值为 nil 表示无匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(x*)b`)
	// Indices:
	//    01234567   012345678
	//    -ab-axb-   -axxb-ab-
	fmt.Println(re.FindSubmatchIndex([]byte("-ab-")))
	fmt.Println(re.FindSubmatchIndex([]byte("-axxb-")))
	fmt.Println(re.FindSubmatchIndex([]byte("-ab-axb-")))
	fmt.Println(re.FindSubmatchIndex([]byte("-axxb-ab-")))
	fmt.Println(re.FindSubmatchIndex([]byte("-foo-")))
}

Output:

[1 3 2 2]
[1 5 2 4]
[1 3 2 2]
[1 5 2 4]
[]

func (*Regexp) LiteralPrefix

func (re *Regexp) LiteralPrefix() (prefix string, complete bool)

LiteralPrefix 返回正则表达式 re 的任何匹配都必须以其开头的字面字符串。 如果该字面字符串构成了整个正则表达式,则返回布尔值 true。

func (*Regexp) Longest

func (re *Regexp) Longest()

Longest 使后续搜索优先选择最左最长匹配。 也就是说,在匹配文本时,该正则表达式返回在输入中尽可能早开始的匹配(最左匹配), 并在这些匹配中选择尽可能长的匹配。 此方法会修改 Regexp,不能与任何其他方法并发调用。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(|b)`)
	fmt.Println(re.FindString("ab"))
	re.Longest()
	fmt.Println(re.FindString("ab"))
}

Output:

a
ab

func (*Regexp) MarshalText

func (re *Regexp) MarshalText() ([]byte, error)

MarshalText 实现了 encoding.TextMarshaler。输出与调用 Regexp.AppendText 方法的结果相同。

更多信息请参阅 Regexp.AppendText

func (*Regexp) Match

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

Match 报告字节切片 b 中是否包含正则表达式 re 的任何匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`foo.?`)
	fmt.Println(re.Match([]byte(`seafood fool`)))
	fmt.Println(re.Match([]byte(`something else`)))

}

Output:

true
false

func (*Regexp) MatchReader

func (re *Regexp) MatchReader(r io.RuneReader) bool

MatchReader 报告 io.RuneReader 返回的文本中是否包含正则表达式 re 的任何匹配。

func (*Regexp) MatchString

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

MatchString 报告字符串 s 中是否包含正则表达式 re 的任何匹配。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(gopher){2}`)
	fmt.Println(re.MatchString("gopher"))
	fmt.Println(re.MatchString("gophergopher"))
	fmt.Println(re.MatchString("gophergophergopher"))
}

Output:

false
true
true

func (*Regexp) NumSubexp

func (re *Regexp) NumSubexp() int

NumSubexp 返回此 Regexp 中带括号的子表达式的数量。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re0 := regexp.MustCompile(`a.`)
	fmt.Printf("%d\n", re0.NumSubexp())

	re := regexp.MustCompile(`(.*)((a)b)(.*)a`)
	fmt.Println(re.NumSubexp())
}

Output:

0
4

func (*Regexp) ReplaceAll

func (re *Regexp) ReplaceAll(src, repl []byte) []byte

ReplaceAll 返回 src 的副本,将 Regexp 的匹配替换为替换文本 repl。 在 repl 中,$ 符号按照 Regexp.Expand 中的规则进行解释。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(x*)b`)
	fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("T")))
	fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1")))
	fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("$1W")))
	fmt.Printf("%s\n", re.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W")))

	re2 := regexp.MustCompile(`a(?P<1W>x*)b`)
	fmt.Printf("%s\n", re2.ReplaceAll([]byte("-ab-axxb-"), []byte("$1W")))
	fmt.Printf("%s\n", re2.ReplaceAll([]byte("-ab-axxb-"), []byte("${1}W")))

}

Output:

-T-T-
--xx-
---
-W-xxW-
--xx-
-W-xxW-

func (*Regexp) ReplaceAllFunc

func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte

ReplaceAllFunc 返回 src 的副本,其中 Regexp 的所有匹配都被函数 repl 应用于匹配字节切片后的返回值所替换。repl 返回的替换值被直接替换, 不使用 Regexp.Expand

func (*Regexp) ReplaceAllLiteral

func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte

ReplaceAllLiteral 返回 src 的副本,将 Regexp 的匹配替换为替换字节 repl。 替换字节 repl 被直接替换,不使用 Regexp.Expand

func (*Regexp) ReplaceAllLiteralString

func (re *Regexp) ReplaceAllLiteralString(src, repl string) string

ReplaceAllLiteralString 返回 src 的副本,将 Regexp 的匹配替换为替换字符串 repl。 替换字符串 repl 被直接替换,不使用 Regexp.Expand

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(x*)b`)
	fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "T"))
	fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "$1"))
	fmt.Println(re.ReplaceAllLiteralString("-ab-axxb-", "${1}"))
}

Output:

-T-T-
-$1-$1-
-${1}-${1}-

func (*Regexp) ReplaceAllString

func (re *Regexp) ReplaceAllString(src, repl string) string

ReplaceAllString 返回 src 的副本,将 Regexp 的匹配替换为替换字符串 repl。 在 repl 中,$ 符号按照 Regexp.Expand 中的规则进行解释。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`a(x*)b`)
	fmt.Println(re.ReplaceAllString("-ab-axxb-", "T"))
	fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1"))
	fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1W"))
	fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W"))

	re2 := regexp.MustCompile(`a(?P<1W>x*)b`)
	fmt.Printf("%s\n", re2.ReplaceAllString("-ab-axxb-", "$1W"))
	fmt.Println(re.ReplaceAllString("-ab-axxb-", "${1}W"))

}

Output:

-T-T-
--xx-
---
-W-xxW-
--xx-
-W-xxW-

func (*Regexp) ReplaceAllStringFunc

func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string

ReplaceAllStringFunc 返回 src 的副本,其中 Regexp 的所有匹配都被函数 repl 应用于匹配子串后的返回值所替换。repl 返回的替换值被直接替换, 不使用 Regexp.Expand

Example
package main

import (
	"fmt"
	"regexp"
	"strings"
)

func main() {
	re := regexp.MustCompile(`[^aeiou]`)
	fmt.Println(re.ReplaceAllStringFunc("seafood fool", strings.ToUpper))
}

Output:

SeaFooD FooL

func (*Regexp) Split

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

Split 将 s 按表达式分隔为子串,并返回这些表达式匹配之间的子串切片。

此方法返回的切片由 s 中未包含在 Regexp.FindAllString 返回的切片中的 所有子串组成。当对不含元字符的表达式调用时,它等价于 strings.SplitN

示例:

s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5)
// s: ["", "b", "b", "c", "cadaaae"]

count 参数决定返回的子串数量:

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	a := regexp.MustCompile(`a`)
	fmt.Println(a.Split("banana", -1))
	fmt.Println(a.Split("banana", 0))
	fmt.Println(a.Split("banana", 1))
	fmt.Println(a.Split("banana", 2))
	zp := regexp.MustCompile(`z+`)
	fmt.Println(zp.Split("pizza", -1))
	fmt.Println(zp.Split("pizza", 0))
	fmt.Println(zp.Split("pizza", 1))
	fmt.Println(zp.Split("pizza", 2))
}

Output:

[b n n ]
[]
[banana]
[b nana]
[pi a]
[]
[pizza]
[pi a]

func (*Regexp) String

func (re *Regexp) String() string

String 返回用于编译该正则表达式的源文本。

func (*Regexp) SubexpIndex

func (re *Regexp) SubexpIndex(name string) int

SubexpIndex 返回具有给定名称的第一个子表达式的索引, 如果没有该名称的子表达式则返回 -1。

注意,多个子表达式可以使用相同的名称,例如 (?P<bob>a+)(?P<bob>b+) 声明了两个名为 "bob" 的子表达式。 在这种情况下,SubexpIndex 返回正则表达式中最左边的此类子表达式的索引。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)`)
	fmt.Println(re.MatchString("Alan Turing"))
	matches := re.FindStringSubmatch("Alan Turing")
	lastIndex := re.SubexpIndex("last")
	fmt.Printf("last => %d\n", lastIndex)
	fmt.Println(matches[lastIndex])
}

Output:

true
last => 2
Turing

func (*Regexp) SubexpNames

func (re *Regexp) SubexpNames() []string

SubexpNames 返回此 Regexp 中带括号的子表达式的名称。 第一个子表达式的名称是 names[1], 因此如果 m 是一个匹配切片,m[i] 的名称就是 SubexpNames()[i]。 由于 Regexp 整体不能命名,names[0] 总是空字符串。 返回的切片不应被修改。

Example
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?P<first>[a-zA-Z]+) (?P<last>[a-zA-Z]+)`)
	fmt.Println(re.MatchString("Alan Turing"))
	fmt.Printf("%q\n", re.SubexpNames())
	reversed := fmt.Sprintf("${%s} ${%s}", re.SubexpNames()[2], re.SubexpNames()[1])
	fmt.Println(reversed)
	fmt.Println(re.ReplaceAllString("Alan Turing", reversed))
}

Output:

true
["" "first" "last"]
${last} ${first}
Turing Alan

func (*Regexp) UnmarshalText

func (re *Regexp) UnmarshalText(text []byte) error

UnmarshalText 通过对编码值调用 Compile 来实现 encoding.TextUnmarshaler

Directories

syntax Package syntax 将正则表达式解析为解析树,并将解析树编译为程序。