1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| re := regexp.MustCompile("fo.?") fmt.Printf("%q\n", re.FindString("seafood")) //"foo" fmt.Printf("%q\n", re.Find([]byte("seafood"))) //"foo"
re := regexp.MustCompile("a.") fmt.Println(re.FindAllString("paranormal", -1)) //-1表示返回所有匹配的值,[ar an al] fmt.Println(re.FindAllString("paranormal", 2)) //2表示返回2个匹配的值,[ar an] fmt.Println(re.FindAllString("paranormal", 1)) //1表示返回1个匹配的值,[ar] fmt.Println(re.FindAllString("paranormal", 0)) //0表示返回0个匹配的值,[] fmt.Println(re.FindAllString("graal", -1)) //[aa] fmt.Println(re.FindAllString("none", -1)) //[]
re := regexp.MustCompile("ab?") fmt.Println(re.FindStringIndex("tablett")) //[1 3],表示匹配到的字符在"tablett"的[1,3]切片处 fmt.Println(re.FindStringIndex("foo") == nil) //没有匹配到会返回nil fmt.Println(re.FindIndex([]byte("tablett"))) //[1 3],表示匹配到的字符在"tablett"的[1,3]切片处 fmt.Println(re.FindIndex([]byte("foo")) == nil) //没有匹配到会返回nil
re := regexp.MustCompile("a.") fmt.Println(re.FindAllStringIndex("paranormal", -1)) //[[1 3] [3 5] [8 10]],-1表示匹配全部 fmt.Println(re.FindAllStringIndex("paranormal", 2)) //[[1 3] [3 5]] ,2表示返回匹配的两个的索引切片 fmt.Println(re.FindAllStringIndex("paranormal", 1)) //[[1 3]] ,1表示返回匹配的一个的索引切片 fmt.Println(re.FindAllIndex([]byte("paranormal"), -1)) //[[1 3] [3 5] [8 10]] fmt.Println(re.FindAllIndex([]byte("paranormal"), 2)) //[[1 3] [3 5]] fmt.Println(re.FindAllIndex([]byte("paranormal"), 1)) //[[1 3]]
re := regexp.MustCompile("a(x*)b(y|z)c") fmt.Printf("%q\n", re.FindSubmatch([]byte("-axxxbyc-"))) //["axxxbyc" "xxx" "y"] fmt.Printf("%q\n", re.FindSubmatch([]byte("-abzc-"))) //["abzc" "" "z"] fmt.Printf("%q\n", re.FindSubmatch([]byte("-aczc-"))) //[],整个都不匹配,更没有分组匹配,将返回空数组 fmt.Printf("%q\n", re.FindStringSubmatch("-axxxbyc-")) //["axxxbyc" "xxx" "y"] fmt.Printf("%q\n", re.FindStringSubmatch("-abzc-")) //["abzc" "" "z"] fmt.Printf("%q\n", re.FindStringSubmatch("-aczc-")) //[],整个都不匹配,更没有分组匹配,将返回空数组
re := regexp.MustCompile("a(x*)b(y|z)c") fmt.Printf("%q\n", re.FindAllSubmatch([]byte("-axxxbyc-axxbyc-axbyc-"), -1)) //[["axxxbyc" "xxx" "y"] ["axxbyc" "xx" "y"] ["axbyc" "x" "y"]] fmt.Printf("%q\n", re.FindAllSubmatch([]byte("-axxxbyc-axxbyc-axbyc-"), 2)) //[["axxxbyc" "xxx" "y"] ["axxbyc" "xx" "y"]] fmt.Printf("%q\n", re.FindAllSubmatch([]byte("-axxxbyc-axxbyc-axbyc-"), 1)) //[["axxxbyc" "xxx" "y"]] fmt.Printf("%q\n", re.FindAllSubmatch([]byte("-abzc-abzc-"), -1)) //[["abzc" "" "z"] ["abzc" "" "z"]] fmt.Printf("%q\n", re.FindAllSubmatch([]byte("-abzc-abzc-"), 1)) //[["abzc" "" "z"]] fmt.Printf("%q\n", re.FindAllSubmatch([]byte("-aczc-"), -1)) //[],整个都不匹配,更没有分组匹配,将返回空数组
fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxxbyc-axxbyc-axbyc-", -1)) //[["axxxbyc" "xxx" "y"] ["axxbyc" "xx" "y"] ["axbyc" "x" "y"]] fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxxbyc-axxbyc-axbyc-", 2)) //[["axxxbyc" "xxx" "y"] ["axxbyc" "xx" "y"]] fmt.Printf("%q\n", re.FindAllStringSubmatch("-axxxbyc-axxbyc-axbyc-", 1)) //[["axxxbyc" "xxx" "y"]] fmt.Printf("%q\n", re.FindAllStringSubmatch("-abzc-abzc-", -1)) //[["abzc" "" "z"] ["abzc" "" "z"]] fmt.Printf("%q\n", re.FindAllStringSubmatch("-abzc-abzc-", 1)) //[["abzc" "" "z"]] fmt.Printf("%q\n", re.FindAllStringSubmatch("-aczc-", -1)) //[],整个都不匹配,更没有分组匹配,将返回空数组
re := regexp.MustCompile("a(x*)b(y|z)c") fmt.Println(re.FindSubmatchIndex([]byte("-axxxbyc-"))) //[1 8 2 5 6 7],即整体匹配字符串"-axxxbyc-"的[1,8]切片,分组1匹配[2,5],分组2匹配[6,7]切片 fmt.Println(re.FindSubmatchIndex([]byte("-abzc-"))) //[1 5 2 2 3 4] fmt.Println(re.FindSubmatchIndex([]byte("-aczc-"))) //[],整个都不匹配,更没有分组匹配,将返回空数组 fmt.Println(re.FindStringSubmatchIndex("-axxxbyc-")) //[1 8 2 5 6 7] fmt.Println(re.FindStringSubmatchIndex("-abzc-")) //[1 5 2 2 3 4] fmt.Println(re.FindStringSubmatchIndex("-aczc-")) //[],整个都不匹配,更没有分组匹配,将返回空数组
re := regexp.MustCompile("a(x*)b(y|z)c") fmt.Println(re.FindAllSubmatchIndex([]byte("-axxxbyc-axxbyc-axbyc-"), -1)) //[[1 8 2 5 6 7] [9 15 10 12 13 14] [16 21 17 18 19 20]] fmt.Println(re.FindAllSubmatchIndex([]byte("-axxxbyc-axxbyc-axbyc-"), 2)) //[[1 8 2 5 6 7] [9 15 10 12 13 14]] fmt.Println(re.FindAllSubmatchIndex([]byte("-axxxbyc-axxbyc-axbyc-"), 1)) //[[1 8 2 5 6 7]] fmt.Println(re.FindAllSubmatchIndex([]byte("-abzc-abzc-"), -1)) //[[1 5 2 2 3 4] [6 10 7 7 8 9]] fmt.Println(re.FindAllSubmatchIndex([]byte("-abzc-abzc-"), 1)) //[[1 5 2 2 3 4]] fmt.Println(re.FindAllSubmatchIndex([]byte("-aczc-"), -1)) //[],整个都不匹配,更没有分组匹配,将返回空数组
fmt.Println(re.FindAllStringSubmatchIndex("-axxxbyc-axxbyc-axbyc-", -1)) //[[1 8 2 5 6 7] [9 15 10 12 13 14] [16 21 17 18 19 20]] fmt.Println(re.FindAllStringSubmatchIndex("-axxxbyc-axxbyc-axbyc-", 2)) //[[1 8 2 5 6 7] [9 15 10 12 13 14]] fmt.Println(re.FindAllStringSubmatchIndex("-axxxbyc-axxbyc-axbyc-", 1)) //[[1 8 2 5 6 7]] fmt.Println(re.FindAllStringSubmatchIndex("-abzc-abzc-", -1)) //[[1 5 2 2 3 4] [6 10 7 7 8 9]] fmt.Println(re.FindAllStringSubmatchIndex("-abzc-abzc-", 1)) //[[1 5 2 2 3 4]] fmt.Println(re.FindAllStringSubmatchIndex("-aczc-", -1)) //[],整个都不匹配,更没有分组匹配,将返回空数组
|