golang正则regexp包使用-03-查找匹配字串,查找匹配字串位置,正则分组(FindSubMatch系列方法,FindSubmatchIndex系列方法)

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.239.11
10
10
239
11
  • 注意
    正则贪婪匹配也是在整个正则能匹配到结果的基础上。

正则我们写的是: (.*)\.(.*)\.(.*)\.(.*) 。前边我们知道.*是贪婪匹配,但是组一的值却取到了 10,而不是10.10.239
试想,如果组一匹配了10.10.239那么第二组将什么都匹配不到,那么整个正则也匹配不到任何值了。

如果我们把正则如下写:

pattern := regexp.MustCompile("(.*)\\.")

则输入为:

10.10.239.
10.10.239

可见,这次是贪婪匹配到了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"
	//myReader := bytes.NewReader([]byte(myString))
	a := pattern.FindStringSubmatch(myString)
	for _,i := range a {
    
    
		fmt.Printf("%s\n",i)
	}
}
  • 结果
10.10.239.11
10
10
239
11

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"
	//r := bytes.NewReader([]byte(myString))
	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]]

猜你喜欢

转载自blog.csdn.net/xingzuo_1840/article/details/125361920