GO語言"regexp"包中"Regexp.Split"類型的用法及代碼示例。
用法:
func(re *Regexp) Split(s string, n int) []string
將切片 s 拆分為由表達式分隔的子字符串,並返回這些表達式匹配之間的子字符串切片。
此方法返回的切片由 FindAllString 返回的切片中不包含的所有 s 子字符串組成,當在不包含元字符的表達式上調用時,它等效於字符串 SplitN
例子:
s := regexp.MustCompile("a*").Split("abaabaccadaaae", 5)
// s: ["", "b", "b", "c", "cadaaae"]
計數確定要返回的子字符串的數量:
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 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))
}
輸出:
[b n n ] [] [banana] [b nana] [pi a] [] [pizza] [pi a]
相關用法
- GO Regexp.SubexpIndex用法及代碼示例
- GO Regexp.SubexpNames用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO Regexp.FindAllStringSubmatch用法及代碼示例
- GO Regexp.Match用法及代碼示例
- GO Regexp.Longest用法及代碼示例
- GO Regexp.FindStringIndex用法及代碼示例
- GO Regexp.ReplaceAll用法及代碼示例
- GO Regexp.Expand用法及代碼示例
- GO Regexp.Find用法及代碼示例
- GO Regexp.FindAllSubmatchIndex用法及代碼示例
- GO Regexp.FindSubmatchIndex用法及代碼示例
- GO Regexp.FindAllStringSubmatchIndex用法及代碼示例
- GO Regexp.MatchString用法及代碼示例
- GO Regexp.ReplaceAllString用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Regexp.Split。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。