<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # package regexp `import "regexp"` regexp包實現了正則表達式搜索。 正則表達式采用RE2語法(除了\c、\C),和Perl、Python等語言的正則基本一致。 參見[http://code.google.com/p/re2/wiki/Syntax](http://code.google.com/p/re2/wiki/Syntax)。 ### Syntax 本包采用的正則表達式語法,默認采用perl標志。某些語法可以通過切換解析時的標志來關閉。 單字符: ``` .????????????? 任意字符(標志s==true時還包括換行符) [xyz]????????? 字符族 [^xyz]???????? 反向字符族 \d???????????? Perl預定義字符族 \D???????????? 反向Perl預定義字符族 [:alpha:]????? ASCII字符族 [:^alpha:]???? 反向ASCII字符族 \pN??????????? Unicode字符族(單字符名),參見unicode包 \PN??????????? 反向Unicode字符族(單字符名) \p{Greek}????? Unicode字符族(完整字符名) \P{Greek}????? 反向Unicode字符族(完整字符名) ``` 結合: ``` xy???????????? 匹配x后接著匹配y x|y???? ???????匹配x或y(優先匹配x) ``` 重復: ``` x*???????????? 重復>=0次匹配x,越多越好(優先重復匹配x) x+???????????? 重復>=1次匹配x,越多越好(優先重復匹配x) x?????? ???????0或1次匹配x,優先1次 x{n,m}? ???????n到m次匹配x,越多越好(優先重復匹配x) x{n,}????????? 重復>=n次匹配x,越多越好(優先重復匹配x) x{n}?????????? 重復n次匹配x x*???????????? 重復>=0次匹配x,越少越好(優先跳出重復) x+???????????? 重復>=1次匹配x,越少越好(優先跳出重復) x????????????? 0或1次匹配x,優先0次 x{n,m}???????? n到m次匹配x,越少越好(優先跳出重復) x{n,}????????? 重復>=n次匹配x,越少越好(優先跳出重復) x{n}?????????? 重復n次匹配x ``` 實現的限制:計數格式x{n}等(不包括x*等格式)中n最大值1000。負數或者顯式出現的過大的值會導致解析錯誤,返回ErrInvalidRepeatSize。 分組: ``` (re)?????????? 編號的捕獲分組 (?P<name>re)?? 命名并編號的捕獲分組 (?:re)???????? 不捕獲的分組 (?flags)?????? 設置當前所在分組的標志,不捕獲也不匹配 (?flags:re)??? 設置re段的標志,不捕獲的分組 ``` 標志的語法為xyz(設置)、-xyz(清楚)、xy-z(設置xy,清楚z),標志如下: ``` I????????????? 大小寫敏感(默認關閉) m????????????? ^和$在匹配文本開始和結尾之外,還可以匹配行首和行尾(默認開啟) s????????????? 讓.可以匹配\n(默認關閉) U????????????? 非貪婪的:交換x*和x*?、x+和x+?……的含義(默認關閉) ``` 邊界匹配: ``` ^????????????? 匹配文本開始,標志m為真時,還匹配行首 $????????????? 匹配文本結尾,標志m為真時,還匹配行尾 \A???????????? 匹配文本開始 \b???????????? 單詞邊界(一邊字符屬于\w,另一邊為文首、文尾、行首、行尾或屬于\W) \B???????????? 非單詞邊界 \z???????????? 匹配文本結尾 ``` 轉義序列: ``` \a???????????? 響鈴符(\007) \f???????????? 換紙符(\014) \t???????????? 水平制表符(\011) \n???????????? 換行符(\012) \r???????????? 回車符(\015) \v???????????? 垂直制表符(\013) \123?????????? 八進制表示的字符碼(最多三個數字) \x7F?????????? 十六進制表示的字符碼(必須兩個數字) \x{10FFFF}???? 十六進制表示的字符碼 \*???????????? 字面值'*' \Q...\E??????? 反斜線后面的字符的字面值 ``` 字符族(預定義字符族之外,方括號內部)的語法: ``` x????????????? 單個字符 A-Z??????????? 字符范圍(方括號內部才可以用) \d???????????? Perl字符族 [:foo:]??????? ASCII字符族 \pF??????????? 單字符名的Unicode字符族 \p{Foo}??????? 完整字符名的Unicode字符族 ``` 預定義字符族作為字符族的元素: ``` [\d]?????????? == \d [^\d]????????? == \D [\D]?????????? == \D [^\D]????????? == \d [[:name:]]???? == [:name:] [^[:name:]]??? == [:^name:] [\p{Name}]???? == \p{Name} [^\p{Name}]??? == \P{Name} ``` Perl字符族: ``` \d???????????? == [0-9] \D???????????? == [^0-9] \s???????????? == [\t\n\f\r ] \S???????????? == [^\t\n\f\r ] \w???????????? == [0-9A-Za-z_] \W???????????? == [^0-9A-Za-z_] ``` ASCII字符族: ``` [:alnum:]????? == [0-9A-Za-z] [:alpha:]????? == [A-Za-z] [:ascii:]????? == [\x00-\x7F] [:blank:]????? == [\t ] [:cntrl:]????? == [\x00-\x1F\x7F] [:digit:]????? == [0-9] [:graph:]????? == [!-~] == [A-Za-z0-9!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~] [:lower:]????? == [a-z] [:print:]????? == [ -~] == [ [:graph:]] [:punct:]????? == [!-/:-@[-`{-~] [:space:]????? == [\t\n\v\f\r ] [:upper:]????? == [A-Z] [:word:]?????? == [0-9A-Za-z_] [:xdigit:]???? == [0-9A-Fa-f] ``` 本包的正則表達式保證搜索復雜度為O(n),其中n為輸入的長度。這一點很多其他開源實現是無法保證的。參見: ``` http://swtch.com/~rsc/regexp/regexp1.html ``` 或其他關于自動機理論的書籍。 所有的字符都被視為utf-8編碼的碼值。 Regexp類型提供了多達16個方法,用于匹配正則表達式并獲取匹配的結果。它們的名字滿足如下正則表達式: ``` Find(All)?(String)?(Submatch)?(Index)? ``` 如果'All'出現了,該方法會返回輸入中所有互不重疊的匹配結果。如果一個匹配結果的前后(沒有間隔字符)存在長度為0的成功匹配,該空匹配會被忽略。包含All的方法會要求一個額外的整數參數n,如果n&gt;=0,方法會返回最多前n個匹配結果。 如果'String'出現了,匹配對象為字符串,否則應該是[]byte類型,返回值和匹配對象的類型是對應的。 如果'Submatch'出現了,返回值是表示正則表達式中成功的組匹配(子匹配/次級匹配)的切片。組匹配是正則表達式內部的括號包圍的次級表達式(也被稱為“捕獲分組”),從左到右按左括號的順序編號。,索引0的組匹配為完整表達式的匹配結果,1為第一個分組的匹配結果,依次類推。 如果'Index'出現了,匹配/分組匹配會用輸入流的字節索引對表示result[2\*n:2\*n+1]表示第n個分組匹配的的匹配結果。如果沒有'Index',匹配結果表示為匹配到的文本。如果索引為負數,表示分組匹配沒有匹配到輸入流中的文本。 方法集也有一個用于從RuneReader中讀取文本進行匹配的子集: ``` MatchReader, FindReaderIndex, FindReaderSubmatchIndex ``` 該子集可能會增加。注意正則表達式匹配可能需要檢驗匹配結果前后的文本,因此從RuneReader匹配文本的方法很可能會讀取到遠遠超出返回的結果所在的位置。 (另有幾個其他方法不滿足該方法模式的) Example ``` // 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 * [func QuoteMeta(s string) string](#QuoteMeta) * [func Match(pattern string, b []byte) (matched bool, err error)](#Match) * [func MatchString(pattern string, s string) (matched bool, err error)](#MatchString) * [func MatchReader(pattern string, r io.RuneReader) (matched bool, err error)](#MatchReader) * [type Regexp](#Regexp) * [func Compile(expr string) (\*Regexp, error)](#Compile) * [func CompilePOSIX(expr string) (\*Regexp, error)](#CompilePOSIX) * [func MustCompile(str string) \*Regexp](#MustCompile) * [func MustCompilePOSIX(str string) \*Regexp](#MustCompilePOSIX) * [func (re \*Regexp) String() string](#Regexp.String) * [func (re \*Regexp) LiteralPrefix() (prefix string, complete bool)](#Regexp.LiteralPrefix) * [func (re \*Regexp) Longest()](#Regexp.Longest) * [func (re \*Regexp) NumSubexp() int](#Regexp.NumSubexp) * [func (re \*Regexp) SubexpNames() []string](#Regexp.SubexpNames) * [func (re \*Regexp) Match(b []byte) bool](#Regexp.Match) * [func (re \*Regexp) MatchString(s string) bool](#Regexp.MatchString) * [func (re \*Regexp) MatchReader(r io.RuneReader) bool](#Regexp.MatchReader) * [func (re \*Regexp) Find(b []byte) []byte](#Regexp.Find) * [func (re \*Regexp) FindString(s string) string](#Regexp.FindString) * [func (re \*Regexp) FindIndex(b []byte) (loc []int)](#Regexp.FindIndex) * [func (re \*Regexp) FindStringIndex(s string) (loc []int)](#Regexp.FindStringIndex) * [func (re \*Regexp) FindReaderIndex(r io.RuneReader) (loc []int)](#Regexp.FindReaderIndex) * [func (re \*Regexp) FindSubmatch(b []byte) [][]byte](#Regexp.FindSubmatch) * [func (re \*Regexp) FindStringSubmatch(s string) []string](#Regexp.FindStringSubmatch) * [func (re \*Regexp) FindSubmatchIndex(b []byte) []int](#Regexp.FindSubmatchIndex) * [func (re \*Regexp) FindStringSubmatchIndex(s string) []int](#Regexp.FindStringSubmatchIndex) * [func (re \*Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int](#Regexp.FindReaderSubmatchIndex) * [func (re \*Regexp) FindAll(b []byte, n int) [][]byte](#Regexp.FindAll) * [func (re \*Regexp) FindAllString(s string, n int) []string](#Regexp.FindAllString) * [func (re \*Regexp) FindAllIndex(b []byte, n int) [][]int](#Regexp.FindAllIndex) * [func (re \*Regexp) FindAllStringIndex(s string, n int) [][]int](#Regexp.FindAllStringIndex) * [func (re \*Regexp) FindAllSubmatch(b []byte, n int) [][][]byte](#Regexp.FindAllSubmatch) * [func (re \*Regexp) FindAllStringSubmatch(s string, n int) [][]string](#Regexp.FindAllStringSubmatch) * [func (re \*Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int](#Regexp.FindAllSubmatchIndex) * [func (re \*Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int](#Regexp.FindAllStringSubmatchIndex) * [func (re \*Regexp) Split(s string, n int) []string](#Regexp.Split) * [func (re \*Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte](#Regexp.Expand) * [func (re \*Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte](#Regexp.ExpandString) * [func (re \*Regexp) ReplaceAllLiteral(src, repl []byte) []byte](#Regexp.ReplaceAllLiteral) * [func (re \*Regexp) ReplaceAllLiteralString(src, repl string) string](#Regexp.ReplaceAllLiteralString) * [func (re \*Regexp) ReplaceAll(src, repl []byte) []byte](#Regexp.ReplaceAll) * [func (re \*Regexp) ReplaceAllString(src, repl string) string](#Regexp.ReplaceAllString) * [func (re \*Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte](#Regexp.ReplaceAllFunc) * [func (re \*Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string](#Regexp.ReplaceAllStringFunc) ### Examples * [MatchString](#example-MatchString) * [Regexp.FindAllString](#example-Regexp-FindAllString) * [Regexp.FindAllStringSubmatch](#example-Regexp-FindAllStringSubmatch) * [Regexp.FindAllStringSubmatchIndex](#example-Regexp-FindAllStringSubmatchIndex) * [Regexp.FindString](#example-Regexp-FindString) * [Regexp.FindStringIndex](#example-Regexp-FindStringIndex) * [Regexp.FindStringSubmatch](#example-Regexp-FindStringSubmatch) * [Regexp.ReplaceAllLiteralString](#example-Regexp-ReplaceAllLiteralString) * [Regexp.ReplaceAllString](#example-Regexp-ReplaceAllString) * [Regexp.SubexpNames](#example-Regexp-SubexpNames) * [package](#example-package) ## func [QuoteMeta](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L581 "View Source") ``` func QuoteMeta(s string) string ``` QuoteMeta返回將s中所有正則表達式元字符都進行轉義后字符串。該字符串可以用在正則表達式中匹配字面值s。例如,QuoteMeta(`[foo]`)會返回`\[foo\]`。 ## func [Match](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L433 "View Source") ``` func Match(pattern string, b []byte) (matched bool, err error) ``` Match檢查b中是否存在匹配pattern的子序列。更復雜的用法請使用Compile函數和Regexp對象。 ## func [MatchString](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L422 "View Source") ``` func MatchString(pattern string, s string) (matched bool, err error) ``` MatchString類似Match,但匹配對象是字符串。 Example ``` 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 [MatchReader](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L411 "View Source") ``` func MatchReader(pattern string, r io.RuneReader) (matched bool, err error) ``` MatchReader類似Match,但匹配對象是io.RuneReader。 ## type [Regexp](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L82 "View Source") ``` type Regexp struct { // 內含隱藏或非導出字段 } ``` Regexp代表一個編譯好的正則表達式。Regexp可以被多線程安全地同時使用。 ### func [Compile](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L117 "View Source") ``` func Compile(expr string) (*Regexp, error) ``` Compile解析并返回一個正則表達式。如果成功返回,該Regexp就可用于匹配文本。 在匹配文本時,該正則表達式會盡可能早的開始匹配,并且在匹配過程中選擇回溯搜索到的第一個匹配結果。這種模式被稱為“leftmost-first”,Perl、Python和其他實現都采用了這種模式,但本包的實現沒有回溯的損耗。對POSIX的“leftmost-longest”模式,參見CompilePOSIX。 ### func [CompilePOSIX](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L140 "View Source") ``` func CompilePOSIX(expr string) (*Regexp, error) ``` 類似Compile但會將語法約束到POSIX ERE(egrep)語法,并將匹配模式設置為leftmost-longest。 在匹配文本時,該正則表達式會盡可能早的開始匹配,并且在匹配過程中選擇搜索到的最長的匹配結果。這種模式被稱為“leftmost-longest”,POSIX采用了這種模式(早期正則的DFA自動機模式)。 然而,可能會有多個“leftmost-longest”匹配,每個都有不同的組匹配狀態,本包在這里和POSIX不同。在所有可能的“leftmost-longest”匹配里,本包選擇回溯搜索時第一個找到的,而POSIX會選擇候選結果中第一個組匹配最長的(可能有多個),然后再從中選出第二個組匹配最長的,依次類推。POSIX規則計算困難,甚至沒有良好定義。 參見[http://swtch.com/~rsc/regexp/regexp2.html#posix](http://swtch.com/~rsc/regexp/regexp2.html#posix)獲取細節。 ### func [MustCompile](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L218 "View Source") ``` func MustCompile(str string) *Regexp ``` MustCompile類似Compile但會在解析失敗時panic,主要用于全局正則表達式變量的安全初始化。 ### func [MustCompilePOSIX](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L229 "View Source") ``` func MustCompilePOSIX(str string) *Regexp ``` MustCompilePOSIX類似CompilePOSIX但會在解析失敗時panic,主要用于全局正則表達式變量的安全初始化。 ### func (\*Regexp) [String](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L103 "View Source") ``` func (re *Regexp) String() string ``` String返回用于編譯成正則表達式的字符串。 ### func (\*Regexp) [LiteralPrefix](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L388 "View Source") ``` func (re *Regexp) LiteralPrefix() (prefix string, complete bool) ``` LiteralPrefix返回一個字符串字面值prefix,任何匹配本正則表達式的字符串都會以prefix起始。?如果該字符串字面值包含整個正則表達式,返回值complete會設為真。 ### func (\*Regexp) [NumSubexp](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L245 "View Source") ``` func (re *Regexp) NumSubexp() int ``` NumSubexp返回該正則表達式中捕獲分組的數量。 ### func (\*Regexp) [SubexpNames](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L254 "View Source") ``` func (re *Regexp) SubexpNames() []string ``` SubexpNames返回該正則表達式中捕獲分組的名字。第一個分組的名字是names[1],因此,如果m是一個組匹配切片,m[i]的名字是SubexpNames()[i]。因為整個正則表達式是無法被命名的,names[0]必然是空字符串。該切片不應被修改。 Example ``` 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) [Longest](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L148 "View Source") ``` func (re *Regexp) Longest() ``` Longest讓正則表達式在之后的搜索中都采用"leftmost-longest"模式。在匹配文本時,該正則表達式會盡可能早的開始匹配,并且在匹配過程中選擇搜索到的最長的匹配結果。 ### func (\*Regexp) [Match](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L404 "View Source") ``` func (re *Regexp) Match(b []byte) bool ``` Match檢查b中是否存在匹配pattern的子序列。 ### func (\*Regexp) [MatchString](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L399 "View Source") ``` func (re *Regexp) MatchString(s string) bool ``` MatchString類似Match,但匹配對象是字符串。 ### func (\*Regexp) [MatchReader](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L394 "View Source") ``` func (re *Regexp) MatchReader(r io.RuneReader) bool ``` MatchReader類似Match,但匹配對象是io.RuneReader。 ### func (\*Regexp) [Find](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L663 "View Source") ``` func (re *Regexp) Find(b []byte) []byte ``` Find返回保管正則表達式re在b中的最左側的一個匹配結果的[]byte切片。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindString](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L688 "View Source") ``` func (re *Regexp) FindString(s string) string ``` Find返回保管正則表達式re在b中的最左側的一個匹配結果的字符串。如果沒有匹配到,會返回"";但如果正則表達式成功匹配了一個空字符串,也會返回""。如果需要區分這種情況,請使用FindStringIndex?或FindStringSubmatch。 Example ``` re := regexp.MustCompile("fo.?") fmt.Printf("%q\n", re.FindString("seafood")) fmt.Printf("%q\n", re.FindString("meat")) ``` Output: ``` "foo" "" ``` ### func (\*Regexp) [FindIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L675 "View Source") ``` func (re *Regexp) FindIndex(b []byte) (loc []int) ``` Find返回保管正則表達式re在b中的最左側的一個匹配結果的起止位置的切片(顯然len(loc)==2)。匹配結果可以通過起止位置對b做切片操作得到:b[loc[0]:loc[1]]。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindStringIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L700 "View Source") ``` func (re *Regexp) FindStringIndex(s string) (loc []int) ``` Find返回保管正則表達式re在b中的最左側的一個匹配結果的起止位置的切片(顯然len(loc)==2)。匹配結果可以通過起止位置對b做切片操作得到:b[loc[0]:loc[1]]。如果沒有匹配到,會返回nil。 Example ``` re := regexp.MustCompile("ab?") fmt.Println(re.FindStringIndex("tablett")) fmt.Println(re.FindStringIndex("foo") == nil) ``` Output: ``` [1 3] true ``` ### func (\*Regexp) [FindReaderIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L713 "View Source") ``` func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int) ``` Find返回保管正則表達式re在b中的最左側的一個匹配結果的起止位置的切片(顯然len(loc)==2)。匹配結果可以在輸入流r的字節偏移量loc[0]到loc[1]-1(包括二者)位置找到。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindSubmatch](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L726 "View Source") ``` func (re *Regexp) FindSubmatch(b []byte) [][]byte ``` Find返回一個保管正則表達式re在b中的最左側的一個匹配結果以及(可能有的)分組匹配的結果的[][]byte切片。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindStringSubmatch](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L882 "View Source") ``` func (re *Regexp) FindStringSubmatch(s string) []string ``` Find返回一個保管正則表達式re在b中的最左側的一個匹配結果以及(可能有的)分組匹配的結果的[]string切片。如果沒有匹配到,會返回nil。 Example ``` 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) [FindSubmatchIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L873 "View Source") ``` func (re *Regexp) FindSubmatchIndex(b []byte) []int ``` Find返回一個保管正則表達式re在b中的最左側的一個匹配結果以及(可能有的)分組匹配的結果的起止位置的切片。匹配結果和分組匹配結果可以通過起止位置對b做切片操作得到:b[loc[2\*n]:loc[2\*n+1]]。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindStringSubmatchIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L901 "View Source") ``` func (re *Regexp) FindStringSubmatchIndex(s string) []int ``` Find返回一個保管正則表達式re在b中的最左側的一個匹配結果以及(可能有的)分組匹配的結果的起止位置的切片。匹配結果和分組匹配結果可以通過起止位置對b做切片操作得到:b[loc[2\*n]:loc[2\*n+1]]。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindReaderSubmatchIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L910 "View Source") ``` func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int ``` Find返回一個保管正則表達式re在b中的最左側的一個匹配結果以及(可能有的)分組匹配的結果的起止位置的切片。匹配結果和分組匹配結果可以在輸入流r的字節偏移量loc[0]到loc[1]-1(包括二者)位置找到。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindAll](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L920 "View Source") ``` func (re *Regexp) FindAll(b []byte, n int) [][]byte ``` Find返回保管正則表達式re在b中的所有不重疊的匹配結果的[][]byte切片。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindAllString](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L956 "View Source") ``` func (re *Regexp) FindAllString(s string, n int) []string ``` Find返回保管正則表達式re在b中的所有不重疊的匹配結果的[]string切片。如果沒有匹配到,會返回nil。 Example ``` 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) [FindAllIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L938 "View Source") ``` func (re *Regexp) FindAllIndex(b []byte, n int) [][]int ``` Find返回保管正則表達式re在b中的所有不重疊的匹配結果的起止位置的切片。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindAllStringIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L974 "View Source") ``` func (re *Regexp) FindAllStringIndex(s string, n int) [][]int ``` Find返回保管正則表達式re在b中的所有不重疊的匹配結果的起止位置的切片。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindAllSubmatch](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L992 "View Source") ``` func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte ``` Find返回一個保管正則表達式re在b中的所有不重疊的匹配結果及其對應的(可能有的)分組匹配的結果的[][][]byte切片。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindAllStringSubmatch](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L1034 "View Source") ``` func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string ``` Find返回一個保管正則表達式re在b中的所有不重疊的匹配結果及其對應的(可能有的)分組匹配的結果的[][]string切片。如果沒有匹配到,會返回nil。 Example ``` 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) [FindAllSubmatchIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L1016 "View Source") ``` func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int ``` Find返回一個保管正則表達式re在b中的所有不重疊的匹配結果及其對應的(可能有的)分組匹配的結果的起止位置的切片(第一層表示第幾個匹配結果,完整匹配和分組匹配的起止位置對在第二層)。如果沒有匹配到,會返回nil。 ### func (\*Regexp) [FindAllStringSubmatchIndex](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L1059 "View Source") ``` func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int ``` Find返回一個保管正則表達式re在b中的所有不重疊的匹配結果及其對應的(可能有的)分組匹配的結果的起止位置的切片(第一層表示第幾個匹配結果,完整匹配和分組匹配的起止位置對在第二層)。如果沒有匹配到,會返回nil。 Example ``` 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) [Split](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L1088 "View Source") ``` func (re *Regexp) Split(s string, n int) []string ``` Split將re在s中匹配到的結果作為分隔符將s分割成多個字符串,并返回這些正則匹配結果之間的字符串的切片。 返回的切片不會包含正則匹配的結果,只包含匹配結果之間的片段。當正則表達式re中不含正則元字符時,本方法等價于strings.SplitN。 舉例: ``` s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5) // s: ["", "b", "b", "c", "cadaaae"] ``` 參數n絕對返回的子字符串的數量: ``` n > 0 :?返回最多n個子字符串,最后一個子字符串是剩余未進行分割的部分。 n == 0:?返回nil (zero substrings) n < 0 :?返回所有子字符串 ``` ### func (\*Regexp) [Expand](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L757 "View Source") ``` func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte ``` Expand返回新生成的將template添加到dst后面的切片。在添加時,Expand會將template中的變量替換為從src匹配的結果。match應該是被FindSubmatchIndex返回的匹配結果起止位置索引。(通常就是匹配src,除非你要將匹配得到的位置用于另一個[]byte) 在template參數里,一個變量表示為格式如:$name或${name}的字符串,其中name是長度&gt;0的字母、數字和下劃線的序列。一個單純的數字字符名如$1會作為捕獲分組的數字索引;其他的名字對應(?P&lt;name&gt;...)語法產生的命名捕獲分組的名字。超出范圍的數字索引、索引對應的分組未匹配到文本、正則表達式中未出現的分組名,都會被替換為空切片。 $name格式的變量名,name會盡可能取最長序列:$1x等價于${1x}而非${1}x,$10等價于${10}而非${1}0。因此$name適用在后跟空格/換行等字符的情況,${name}適用所有情況。 如果要在輸出中插入一個字面值'$',在template里可以使用$$。 ### func (\*Regexp) [ExpandString](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L764 "View Source") ``` func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte ``` ExpandString類似Expand,但template和src參數為字符串。它將替換結果添加到切片并返回切片,以便讓調用代碼控制內存申請。 ### func (\*Regexp) [ReplaceAllLiteral](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L556 "View Source") ``` func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte ``` ReplaceAllLiteral返回src的一個拷貝,將src中所有re的匹配結果都替換為repl。repl參數被直接使用,不會使用Expand進行擴展。 ### func (\*Regexp) [ReplaceAllLiteralString](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L458 "View Source") ``` func (re *Regexp) ReplaceAllLiteralString(src, repl string) string ``` ReplaceAllLiteralString返回src的一個拷貝,將src中所有re的匹配結果都替換為repl。repl參數被直接使用,不會使用Expand進行擴展。 Example ``` 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) [ReplaceAll](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L538 "View Source") ``` func (re *Regexp) ReplaceAll(src, repl []byte) []byte ``` ReplaceAllLiteral返回src的一個拷貝,將src中所有re的匹配結果都替換為repl。在替換時,repl中的'$'符號會按照Expand方法的規則進行解釋和替換,例如$1會被替換為第一個分組匹配結果。 ### func (\*Regexp) [ReplaceAllString](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L444 "View Source") ``` func (re *Regexp) ReplaceAllString(src, repl string) string ``` ReplaceAllLiteral返回src的一個拷貝,將src中所有re的匹配結果都替換為repl。在替換時,repl中的'$'符號會按照Expand方法的規則進行解釋和替換,例如$1會被替換為第一個分組匹配結果。 Example ``` 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")) ``` Output: ``` -T-T- --xx- --- -W-xxW- ``` ### func (\*Regexp) [ReplaceAllFunc](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L566 "View Source") ``` func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte ``` ReplaceAllLiteral返回src的一個拷貝,將src中所有re的匹配結果(設為matched)都替換為repl(matched)。repl返回的切片被直接使用,不會使用Expand進行擴展。 ### func (\*Regexp) [ReplaceAllStringFunc](https://github.com/golang/go/blob/master/src/regexp/regexp.go#L468 "View Source") ``` func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string ``` ReplaceAllLiteral返回src的一個拷貝,將src中所有re的匹配結果(設為matched)都替換為repl(matched)。repl返回的字符串被直接使用,不會使用Expand進行擴展。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看