@
1. FindSubMatch系列方法 (查找匹配字串,可分组)
方法 |
匹配字串类型 |
返回值 |
返回类型 |
FindSubmatch |
[ ]byte |
[第一匹配值 值分组1 值分组2 ] |
[ ][ ]byte |
FindSubmatch |
string |
[第一匹配值 值分组1 值分组2 ] |
[ ]string |
FindAllSubmatch() |
[ ]byte |
返回数组,成员为同上每次匹配的数组 |
[ ][ ][ ]byte |
FindAllStringSubmatch() |
string |
返回数组,成员为同上每次匹配的数组 |
[ ][ ]string |
1.1 FindSubMatch()
语法
| func (re *Regexp) FindSubmatch(b []byte) [][]byte |
返回切片从0起,依次是 [正则字串完整匹配 正则中分组一的值 正则中分组2的值 ……]
完整示例
| package main |
| |
| import ( |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(.*)\\.(.*)\\.(.*)\\.(.*)") |
| myString := "10.10.239.11" |
| a := pattern.FindSubmatch([]byte(myString)) |
| for _,i := range a { |
| fmt.Printf("%s\n",string(i)) |
| } |
| } |
- 注意
正则贪婪匹配也是在整个正则能匹配到结果的基础上。
正则我们写的是: (.*)\.(.*)\.(.*)\.(.*)
。前边我们知道.*
是贪婪匹配,但是组一的值却取到了 10
,而不是10.10.239
。
试想,如果组一匹配了10.10.239
那么第二组将什么都匹配不到,那么整个正则也匹配不到任何值了。
如果我们把正则如下写:
| pattern := regexp.MustCompile("(.*)\\.") |
则输入为:
可见,这次是贪婪匹配到了3段地址。(下边几个match系方法与此相同)
1.2 FindStringSubmatch()
语法
| func (re *Regexp) FindStringSubmatch(s string) []string |
完整示例
| package main |
| |
| import ( |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(.*)\\.(.*)\\.(.*)\\.(.*)") |
| myString := "10.10.239.11" |
| |
| a := pattern.FindStringSubmatch(myString) |
| for _,i := range a { |
| fmt.Printf("%s\n",i) |
| } |
| } |
1.3 FindAllSubmatch()
语法
| func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte |
完整示例
| package main |
| |
| import ( |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(.*?)\\.(.*?)") |
| myString := "10.10.239.11" |
| a := pattern.FindAllSubmatch([]byte(myString),-1) |
| fmt.Printf("%s\n",a) |
| } |
| [[10. 10 ] [10. 10 ] [239. 239 ]] |
可以看到
- 取到了3次匹配值,依次为:
[10. 10 ]
[10. 10 ]
[239. 239 ]
- 每一组分别为: [ 第N次匹配值 第N次组一值 第N次组二值]
- 组二在合理范围内最小匹配,取到了空
有人问了:既然是空你还要说?
不是我非要说,是结果给组二留了位置。注意第一组值和"]"之间有空格,那个空格是组一和组二的间隔符。
如果我们把正则写成:
| pattern := regexp.MustCompile("(.*?)\\.") |
那么结果为:
| [[10. 10] [10. 10] [239. 239]] |
将没有这个间隔符的空格。
1.4 FindAllStringSubmatch()
语法
| func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string``` |
| |
完整示例
| package main |
| |
| import ( |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(\\d+)\\.(\\d+)") |
| myString := "10.10.239.11" |
| a := pattern.FindAllStringSubmatch(myString,-1) |
| fmt.Printf("%s\n",a) |
| } |
| [[10.10 10 10] [239.11 239 11]] |
2. FindSubmatchIndex 系列方法
方法 |
匹配字串类型 |
返回值 |
返回类型 |
FindSubmatchIndex() |
[ ]byte |
第一匹配值和分组的位置 |
[ ]int |
FindStringSubmatchIndex() |
string |
第一匹配值和分组的位置 |
[ ]int |
FindReaderSubmatchIndex() |
io.RuneReader |
第一匹配值和分组的位置 |
[ ]int |
FindAllStringSubmatchIndex() |
[ ]byte |
返回数组,成员为同上每次匹配的数组 |
[ ][ ][ ]int |
FindAllStringSubmatchIndex() |
string |
返回数组,成员为同上每次匹配的数组 |
[ ][ ][ ]int |
2.1 FindSubmatchIndex()
语法
| func (re *Regexp) FindSubmatchIndex(b []byte) []int |
完整示例
| package main |
| |
| import ( |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(\\d+)\\.(\\d+)") |
| myString := "10.10.239.11" |
| s := pattern.FindSubmatch([]byte(myString)) |
| p := pattern.FindSubmatchIndex([]byte(myString)) |
| fmt.Printf("匹配到字串为:%s\n位置分别为:%d",s,p) |
| } |
| 匹配到字串为:[10.10 10 10] |
| 位置分别为:[0 5 0 2 3 5] |
结果说明:
- 正则匹配到字串
10.10
,起始于原字串位置 0
,结束于原字串位置 5
之前
- 正则匹配到组一为
10
,起始于原字串位置0
,结束于原字串位置2
之前
- 正则匹配到组二为
10
,起始于原字串位置3
,结束于原字串位置5
之前
2.2 FindStringSubmatchIndex()
语法
| func (re *Regexp) FindStringSubmatchIndex(s string) []int |
完整示例
| package main |
| |
| import ( |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(\\d+)\\.(\\d+)") |
| myString := "10.10.239.11" |
| s := pattern.FindStringSubmatch(myString) |
| p := pattern.FindStringSubmatchIndex(myString) |
| fmt.Printf("匹配到字串为:%s\n位置分别为:%d",s,p) |
| } |
| |
| 匹配到字串为:[10.10 10 10] |
| 位置分别为:[0 5 0 2 3 5] |
2.3 FindReaderSubmatchIndex
语法
| func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int |
完整示例
| package main |
| |
| import ( |
| "bytes" |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(\\d+)\\.(\\d+)") |
| myString := "10.10.239.11" |
| r := bytes.NewReader([]byte(myString)) |
| s := pattern.FindStringSubmatch(myString) |
| p := pattern.FindReaderSubmatchIndex(r) |
| fmt.Printf("匹配到字串为:%s\n位置分别为:%d",s,p) |
| } |
| 匹配到字串为:[10.10 10 10] |
| 位置分别为:[0 5 0 2 3 5] |
2.4 FindAllSubmatchIndex()
语法
| func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int |
完整示例
| package main |
| |
| import ( |
| |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(\\d+)\\.(\\d+)") |
| myString := "10.10.239.11" |
| s := pattern.FindAllSubmatch([]byte(myString),-1) |
| p := pattern.FindAllSubmatchIndex([]byte(myString), -1) |
| fmt.Printf("匹配到字串为:%s\n位置分别为:%d",s,p) |
| } |
| 匹配到字串为:[[10.10 10 10] [239.11 239 11]] |
| 位置分别为:[[0 5 0 2 3 5] [6 12 6 9 10 12]] |
2.5 FindAllStringSubmatchIndex()
语法
| func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int |
完整示例
| package main |
| |
| import ( |
| |
| "fmt" |
| "regexp" |
| ) |
| |
| func main() { |
| pattern := regexp.MustCompile("(\\d+)\\.(\\d+)") |
| myString := "10.10.239.11" |
| |
| s := pattern.FindAllStringSubmatch(myString,-1) |
| p := pattern.FindAllStringSubmatchIndex(myString, -1) |
| fmt.Printf("匹配到字串为:%s\n位置分别为:%d",s,p) |
| } |
| 匹配到字串为:[[10.10 10 10] [239.11 239 11]] |
| 位置分别为:[[0 5 0 2 3 5] [6 12 6 9 10 12]] |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了