亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄 搜尋
archive archive/tar archive/zip bufio bufio(緩存) builtin builtin(內(nèi)置包) bytes bytes(包字節(jié)) compress compress/bzip2(壓縮/bzip2) compress/flate(壓縮/flate) compress/gzip(壓縮/gzip) compress/lzw(壓縮/lzw) compress/zlib(壓縮/zlib) container container/heap(容器數(shù)據(jù)結(jié)構(gòu)heap) container/list(容器數(shù)據(jù)結(jié)構(gòu)list) container/ring(容器數(shù)據(jù)結(jié)構(gòu)ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(數(shù)據(jù)庫(kù)/sql) database/sql/driver(數(shù)據(jù)庫(kù)/sql/driver) debug debug/dwarf(調(diào)試/dwarf) debug/elf(調(diào)試/elf) debug/gosym(調(diào)試/gosym) debug/macho(調(diào)試/macho) debug/pe(調(diào)試/pe) debug/plan9obj(調(diào)試/plan9obj) encoding encoding(編碼) encoding/ascii85(編碼/ascii85) encoding/asn1(編碼/asn1) encoding/base32(編碼/base32) encoding/base64(編碼/base64) encoding/binary(編碼/binary) encoding/csv(編碼/csv) encoding/gob(編碼/gob) encoding/hex(編碼/hex) encoding/json(編碼/json) encoding/pem(編碼/pem) encoding/xml(編碼/xml) errors errors(錯(cuò)誤) expvar expvar flag flag(命令行參數(shù)解析flag包) fmt fmt go go/ast(抽象語(yǔ)法樹(shù)) go/build go/constant(常量) go/doc(文檔) go/format(格式) go/importer go/parser go/printer go/scanner(掃描儀) go/token(令牌) go/types(類(lèi)型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(圖像) image/color(顏色) image/color/palette(調(diào)色板) image/draw(繪圖) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系統(tǒng)) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路徑) plugin plugin(插件) reflect reflect(反射) regexp regexp(正則表達(dá)式) regexp/syntax runtime runtime(運(yùn)行時(shí)) runtime/debug(調(diào)試) runtime/internal/sys runtime/pprof runtime/race(競(jìng)爭(zhēng)) runtime/trace(執(zhí)行追蹤器) sort sort(排序算法) strconv strconv(轉(zhuǎn)換) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系統(tǒng)調(diào)用) testing testing(測(cè)試) testing/iotest testing/quick text text/scanner(掃描文本) text/tabwriter text/template(定義模板) text/template/parse time time(時(shí)間戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "regexp"

  • 概述

  • 索引

  • Examples

  • 子目錄

概述

regexp 包實(shí)現(xiàn)正則表達(dá)式搜索。

接受的正則表達(dá)式的語(yǔ)法與 Perl,Python 和其他語(yǔ)言使用的通用語(yǔ)法相同。更準(zhǔn)確地說(shuō),它是  RE2 接受的語(yǔ)法,并在https://golang.org/s/re2syntax中進(jìn)行了描述,\C除外。有關(guān)語(yǔ)法的概述,請(qǐng)運(yùn)行

go doc regexp/syntax

這個(gè)包提供的正則表達(dá)式實(shí)現(xiàn)保證在輸入大小的時(shí)間線性運(yùn)行。(這是一個(gè)不受大多數(shù)正則表達(dá)式的開(kāi)源實(shí)現(xiàn)保證的屬性。)有關(guān)此屬性的更多信息,請(qǐng)參閱

http://swtch.com/~rsc/regexp/regexp1.html

或任何有關(guān)自動(dòng)化理論的書(shū)。

所有字符都是 UTF-8 編碼的代碼點(diǎn)。

有16種 Regexp 方法匹配正則表達(dá)式并識(shí)別匹配的文本。他們的名字與這個(gè)正則表達(dá)式相匹配:

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

如果存在“全部”,則該例程匹配整個(gè)表達(dá)式的連續(xù)的非重疊匹配。忽略與先前匹配相鄰的空匹配。返回值是一個(gè)包含相應(yīng)的非“全部”例程的連續(xù)返回值的片段。這些例程需要一個(gè)額外的整數(shù)參數(shù) n ;如果 n>= 0 ,該函數(shù)最多返回 n 個(gè)匹配/子匹配。

如果'String'存在,則參數(shù)是一個(gè)字符串; 否則它是一個(gè)字節(jié)片; 返回值會(huì)根據(jù)情況進(jìn)行調(diào)整。

如果存在“Submatch”,則返回值是標(biāo)識(shí)表達(dá)式的連續(xù)子匹配的片。子匹配是正則表達(dá)式內(nèi)的帶圓括號(hào)的子表達(dá)式(也稱(chēng)為捕獲組)的匹配項(xiàng),按左括號(hào)順序從左到右編號(hào)。Submatch 0是整個(gè)表達(dá)式的匹配,submatch 1是第一個(gè)加括號(hào)的子表達(dá)式的匹配,依此類(lèi)推。

如果存在'索引',匹配和子匹配由輸入字符串內(nèi)的字節(jié)索引對(duì)標(biāo)識(shí):result2 * n:2 * n + 1標(biāo)識(shí)第 n 個(gè)子匹配的索引。n == 0的對(duì)表示整個(gè)表達(dá)式的匹配。如果'索引'不存在,則匹配由匹配/子匹配的文本識(shí)別。如果索引是負(fù)數(shù),則意味著子表達(dá)式與輸入中的任何字符串都不匹配。

還有一些方法可以應(yīng)用于從 RuneReader 中讀取文本:

MatchReader, FindReaderIndex, FindReaderSubmatchIndex

這個(gè)集合可能會(huì)增長(zhǎng)。請(qǐng)注意,正則表達(dá)式匹配可能需要檢查文本超出匹配返回的文本,因此符合 RuneReader 的文本的方法可能會(huì)在返回之前任意讀入輸入。

(還有一些其他方法與此模式不匹配。)

package mainimport ("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"))}

索引

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

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

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

  • func QuoteMeta(s string) string

  • type Regexp

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

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

  • func MustCompile(str string) *Regexp

  • func MustCompilePOSIX(str string) *Regexp

  • func (re *Regexp) Copy() *Regexp

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  • func (re *Regexp) Longest()

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

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

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

  • func (re *Regexp) NumSubexp() int

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

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

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

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

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

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

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

  • func (re *Regexp) String() string

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

例子

Package

MatchString

Regexp.FindAllString

Regexp.FindAllStringSubmatch

Regexp.FindAllStringSubmatchIndex

Regexp.FindString

Regexp.FindStringIndex

Regexp.FindStringSubmatch

Regexp.MatchString

Regexp.ReplaceAllLiteralString

Regexp.ReplaceAllString

Regexp.Split

Regexp.SubexpNames

文件包

backtrack.go exec.go onepass.go regexp.go

func Match

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

匹配檢查文本正則表達(dá)式是否與字節(jié)片匹配。更復(fù)雜的查詢(xún)需要使用 Compile 和完整的 Regexp 接口。

func MatchReader

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

MatchReader 檢查文本正則表達(dá)式是否與 RuneReader 讀取的文本匹配。更復(fù)雜的查詢(xún)需要使用 Compile 和完整的 Regexp 接口。

func MatchString

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

MatchString 檢查文本正則表達(dá)式是否匹配字符串。更復(fù)雜的查詢(xún)需要使用 Compile 和完整的 Regexp 接口。

package mainimport ("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)}

func QuoteMeta

func QuoteMeta(s string) string

QuoteMeta 返回一個(gè)字符串,它引用參數(shù)文本中的所有正則表達(dá)式元字符; 返回的字符串是一個(gè)匹配文本文本的正則表達(dá)式。例如,QuoteMeta([foo])返回\[foo\]。

type Regexp

正則表達(dá)式是已編譯正則表達(dá)式的表示形式。除了配置方法(如Longest)之外,Regexp 可以安全地用于多個(gè) goroutine 的并發(fā)使用。

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

func Compile

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

編譯解析一個(gè)正則表達(dá)式,并且如果成功返回一個(gè)可以用來(lái)匹配文本的 Regexp 對(duì)象。

當(dāng)匹配文本時(shí),正則表達(dá)式會(huì)返回一個(gè)盡可能早在輸入中開(kāi)始的匹配(最左邊),并在其中選擇回溯搜索首先找到的匹配。這種所謂的最左邊第一匹配與 Perl,Python 和其他實(shí)現(xiàn)使用的語(yǔ)義相同,盡管這個(gè)包實(shí)現(xiàn)它而沒(méi)有回溯的代價(jià)。對(duì)于 POSIX 最左邊最長(zhǎng)的匹配,請(qǐng)參閱 CompilePOSIX 。

func CompilePOSIX

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

CompilePOSIX 就像 Compile,但將正則表達(dá)式限制為 POSIX ERE(egrep) 語(yǔ)法,并將匹配語(yǔ)義更改為最長(zhǎng)。

也就是說(shuō),當(dāng)匹配文本時(shí),正則表達(dá)式會(huì)返回一個(gè)盡可能早在輸入中開(kāi)始的匹配(最左邊),并且在它們之間選擇盡可能長(zhǎng)的匹配。這種所謂的最左邊最長(zhǎng)匹配與早期正則表達(dá)式實(shí)現(xiàn)使用和 POSIX 指定的語(yǔ)義相同。

但是,可以有多個(gè)最左邊最長(zhǎng)的匹配,具有不同的匹配選項(xiàng),并且此包與 POSIX 不同。在可能的最左邊最長(zhǎng)的匹配中,這個(gè)包選擇一個(gè)回溯搜索首先找到的那個(gè),而 POSIX 指定匹配被選擇為最大化第一個(gè)子表達(dá)的長(zhǎng)度,然后第二個(gè)等等從左到右。POSIX 規(guī)則在計(jì)算上是禁止的,甚至沒(méi)有明確定義。

func MustCompile

func MustCompile(str string) *Regexp

MustCompile 就像編譯,但如果表達(dá)式不能被解析就會(huì)發(fā)生混亂。它簡(jiǎn)化了保存已編譯正則表達(dá)式的全局變量的安全初始化。

func MustCompilePOSIX

func MustCompilePOSIX(str string) *Regexp

MustCompilePOSIX 與 CompilePOSIX 類(lèi)似,但如果表達(dá)式無(wú)法解析,則會(huì)發(fā)生混亂。它簡(jiǎn)化了保存已編譯正則表達(dá)式的全局變量的安全初始化。

func (*Regexp) Copy

func (re *Regexp) Copy() *Regexp

復(fù)制返回從 re 復(fù)制的新 Regexp 對(duì)象。

在多個(gè) goroutine 中使用正則表達(dá)式時(shí),給每個(gè) goroutine 自己的副本有助于避免鎖定爭(zhēng)用。

func (*Regexp) Expand

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

展開(kāi)將模板附加到 dst 并返回結(jié)果; 在追加期間,Expand 使用從 src 中抽取的相應(yīng)匹配替換模板中的變量。匹配切片應(yīng)該由 FindSubmatchIndex 返回。

在模板中,變量由 $ name 或 $ {name} 形式的子字符串表示,其中 name 是字母,數(shù)字和下劃線的非空序列。像 $ 1 這樣的純數(shù)字名稱(chēng)指的是具有相應(yīng)索引的子匹配; 其他名稱(chēng)指的是捕獲用(?P <name> ...)語(yǔ)法命名的括號(hào)。對(duì)超出范圍或不匹配的索引或正則表達(dá)式中不存在的名稱(chēng)的引用將替換為空片段。

在 $ name 表單中,姓名應(yīng)盡可能長(zhǎng):$ 1x等同于$ {1x},而不是$ {1} x,而$ 10等同于$ {10},而不是$ {1} 0 。

要在輸出中插入文字$,請(qǐng)?jiān)谀0逯惺褂?$。

func (*Regexp) ExpandString

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

ExpandString 與 Expand 類(lèi)似,但模板和源代碼都是字符串。它附加并返回一個(gè)字節(jié)片段,以便調(diào)用代碼控制分配。

func (*Regexp) Find

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

查找返回一個(gè)片段,其中包含正則表達(dá)式b中最左邊匹配的文本。返回值 nil 表示不匹配。

func (*Regexp) FindAll

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

FindAll 是 Find 的所有版本;它返回表達(dá)式的所有連續(xù)匹配的一部分,如包注釋中的 'All' 描述所定義。返回值 nil 表示不匹配。

func (*Regexp) FindAllIndex

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

FindAllIndex 是 FindIndex 的 'All' 版本; 它返回表達(dá)式的所有連續(xù)匹配的一部分,如包注釋中的 'All' 描述所定義。返回值 nil 表示不匹配。

func (*Regexp) FindAllString

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

FindAllString 是 FindString 的 'All'版本;它返回表達(dá)式的所有連續(xù)匹配的一部分,如包注釋中的 'All' 描述所定義。返回值 nil 表示不匹配。

package mainimport ("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))}

func (*Regexp) FindAllStringIndex

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

FindAllStringIndex 是 FindStringIndex 的 'All' 版本; 它返回表達(dá)式的所有連續(xù)匹配的一部分,如包注釋中的 'All' 描述所定義。返回值 nil 表示不匹配。

func (*Regexp) FindAllStringSubmatch

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

FindAllStringSubmatch 是 FindStringSubmatch 的 'All' 版本; 它返回表達(dá)式的所有連續(xù)匹配的一部分,如包注釋中的 'All' 描述所定義。返回值 nil 表示不匹配。

package mainimport ("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))}

func (*Regexp) FindAllStringSubmatchIndex

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

FindAllStringSubmatchIndex 是 FindStringSubmatchIndex 的 'All' 版本; 它返回表達(dá)式的所有連續(xù)匹配的一部分,如包注釋中的 'All' 描述所定義。返回值 nil 表示不匹配。

package mainimport ("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))}

func (*Regexp) FindAllSubmatch

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

FindAllSubmatch 是 FindSubmatch 的 'All' 版本; 它返回表達(dá)式的所有連續(xù)匹配的一部分,如包注釋中的 'All' 描述所定義。返回值 nil 表示不匹配。

func (*Regexp) FindAllSubmatchIndex

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

FindAllSubmatchIndex 是 FindSubmatchIndex 的 'All' 版本; 它返回表達(dá)式的所有連續(xù)匹配的一部分,如包注釋中的 'All' 描述所定義。返回值 nil 表示不匹配。

func (*Regexp) FindIndex

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

FindIndex 返回一個(gè)兩個(gè)元素的整數(shù)切片,用于定義正則表達(dá)式 b 中最左邊匹配的位置。匹配本身在 b  [loc0:loc1]。返回值 nil 表示不匹配。

func (*Regexp) FindReaderIndex

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

FindReaderIndex 返回一個(gè)由元素組成的雙元素切片,用于定義從 RuneReader 讀取的文本中正則表達(dá)式最左邊匹配的位置。在字節(jié)偏移量 loc0 到 loc1-1 的輸入流中找到匹配文本。返回值 nil 表示不匹配。

func (*Regexp) FindReaderSubmatchIndex

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

FindReaderSubmatchIndex 返回一個(gè)片段,它保存標(biāo)識(shí)由 RuneReader 讀取的文本正則表達(dá)式的最左邊匹配的索引對(duì),以及它的子表達(dá)式的匹配(如果有的話),如反饋中的 “Submatch” 和 “Index” 描述。返回值 nil 表示不匹配。

func (*Regexp) FindString

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

FindString 返回一個(gè)字符串,其中包含正則表達(dá)式的s中最左邊匹配的文本。如果不匹配,則返回值為空字符串,但如果正則表達(dá)式成功匹配空字符串,則它也將為空。如果有必要區(qū)分這些情況,請(qǐng)使用 FindStringIndex 或 FindStringSubmatch 。

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

func (*Regexp) FindStringIndex

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

FindStringIndex 返回一個(gè)整數(shù)的兩個(gè)元素切片,用于定義正則表達(dá)式的 s 中最左邊匹配的位置。匹配本身在 s [loc0:loc1] 。返回值 nil 表示不匹配。

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

func (*Regexp) FindStringSubmatch

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

FindStringSubmatch 返回一段字符串,其中包含 s 中正則表達(dá)式最左邊匹配的文本以及其子表達(dá)式的匹配(如果有),如反饋中的 'Submatch' 描述所定義。返回值 nil 表示不匹配。

package mainimport ("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-"))}

func (*Regexp) FindStringSubmatchIndex

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

FindStringSubmatchIndex 返回一個(gè)片段,它持有標(biāo)識(shí) s 中正則表達(dá)式的最左邊匹配的索引對(duì),以及它的子表達(dá)式的匹配(如果有的話),如包注釋中的 'Submatch' 和 'Index' 描述所定義的。返回值 nil 表示不匹配。

func (*Regexp) FindSubmatch

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

FindSubmatch 返回一片切片,其中包含 b 中正則表達(dá)式最左邊匹配的文本以及其子表達(dá)式的匹配(如果有),如包注釋中的 'Submatch' 描述所定義。返回值 nil 表示不匹配。

func (*Regexp) FindSubmatchIndex

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

FindSubmatchIndex 返回一個(gè)片段,該片段包含標(biāo)識(shí) b 中正則表達(dá)式的最左邊匹配的索引對(duì)以及它的子表達(dá)式的匹配(如果有的話),如注釋中的 'Submatch' 和 'Index' 描述所定義的。返回值 nil 表示不匹配。

func (*Regexp) LiteralPrefix

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

LiteralPrefix 返回一個(gè)文字字符串,它必須開(kāi)始正則表達(dá)式 re 的任何匹配。如果文字字符串包含整個(gè)正則表達(dá)式,它將返回 boolean 值 true 。

func (*Regexp) Longest

func (re *Regexp) Longest()

最長(zhǎng)使得未來(lái)的搜索更喜歡最左邊最長(zhǎng)的匹配。也就是說(shuō),當(dāng)匹配文本時(shí),正則表達(dá)式會(huì)返回一個(gè)盡可能早在輸入中開(kāi)始的匹配(最左邊),并且在它們之間選擇盡可能長(zhǎng)的匹配。此方法修改正則表達(dá)式,可能不會(huì)與任何其他方法同時(shí)調(diào)用。

func (*Regexp) Match

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

匹配報(bào)告正則表達(dá)式匹配字節(jié)片段 b 。

func (*Regexp) MatchReader

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

MatchReader 報(bào)告正則表達(dá)式是否匹配由符文閱讀器讀取的文本。

func (*Regexp) MatchString

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

MatchString 報(bào)告 Regexp 是否匹配字符串 s 。

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

func (*Regexp) NumSubexp

func (re *Regexp) NumSubexp() int

NumSubexp 返回此正則表達(dá)式中帶括號(hào)的子表達(dá)式的數(shù)量。

func (*Regexp) ReplaceAll

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

ReplaceAll 返回 src 的一個(gè)副本,用替換文本 repl 替換正則表達(dá)式的匹配。在內(nèi)部 repl 中, $ 符號(hào)被解釋為在 Expand 中,因此例如 $ 1 代表第一個(gè)子匹配的文本。

func (*Regexp) ReplaceAllFunc

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

ReplaceAllFunc 返回一個(gè) src 的副本,其中 Regexp 的所有匹配已被替換為應(yīng)用于匹配字節(jié)片的函數(shù) repl 的返回值。由 repl 返回的替換被直接替換,而不使用 Expand 。

func (*Regexp) ReplaceAllLiteral

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

ReplaceAllLiteral 返回 src 的一個(gè)副本,將 Regexp 的匹配替換為替換字節(jié) repl 。替換 repl 被直接替換,而不使用 Expand 。

func (*Regexp) ReplaceAllLiteralString

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

ReplaceAllLiteralString 返回 src 的副本,用替換字符串 repl 替換 Regexp 的匹配項(xiàng)。替換 repl 被直接替換,而不使用 Expand 。

package mainimport ("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}"))}

func (*Regexp) ReplaceAllString

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

ReplaceAllString 返回 src 的副本,用替換字符串 repl 替換正則表達(dá)式的匹配項(xiàng)。在內(nèi)部 repl 中,$ 符號(hào)被解釋為在 Expand 中,因此例如 $ 1 代表第一個(gè)子匹配的文本。

package mainimport ("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"))}

func (*Regexp) ReplaceAllStringFunc

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

ReplaceAllStringFunc 返回一個(gè) src 的副本,其中 Regexp 的所有匹配已被替換為應(yīng)用于匹配子字符串的函數(shù) repl 的返回值。由 repl 返回的替換被直接替換,而不使用 Expand 。

func (*Regexp) Split

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

將切片 s 分割成由表達(dá)式分隔的子字符串,并在這些表達(dá)式匹配之間返回一個(gè)子字符串片段。

此方法返回的片包含所有沒(méi)有包含在由 FindAllString 返回的片中的子串。當(dāng)調(diào)用一個(gè)不包含元字符的表達(dá)式時(shí),它相當(dāng)于 strings.SplitN 。

例:

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

計(jì)數(shù)決定要返回的子字符串的數(shù)量:

n > 0: at most n substrings; the last substring will be the unsplit remainder.n == 0: the result is nil (zero substrings)n < 0: all substrings

package mainimport ("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))}

func (*Regexp) String

func (re *Regexp) String() string

字符串返回用于編譯正則表達(dá)式的源文本。

func (*Regexp) SubexpNames

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

SubexpNames 返回此 Regexp 中帶括號(hào)的子表達(dá)式的名稱(chēng)。第一個(gè)子表達(dá)式的名稱(chēng)是 names1 ,因此如果 m 是匹配片,則 mi 的名稱(chēng)是 SubexpNames()i 。由于整個(gè)正則表達(dá)式不能被命名,names0 總是空字符串。slice 不應(yīng)該被修改。

package mainimport ("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))}

子目錄

Name

Synopsis

syntax

包語(yǔ)法將正則表達(dá)式解析為解析樹(shù)并將解析樹(shù)編譯為程序。

上一篇: 下一篇: