当前位置:网站首页>Golang regular regexp package uses -05- extend expand(), cut split() according to the rule

Golang regular regexp package uses -05- extend expand(), cut split() according to the rule

2022-06-25 15:36:00 Development, operation and maintenance Xuande company

1. Expand

1.1 Expand() Method

grammar

func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte

explain

  • take template The results of are added to dst Back
  • template There is $1$2 ……, These variables can be in src Find .
  • match It's right src Resolution of corresponding group .

Complete example

package main

import (
	"fmt"
	"regexp"
)

func main() {
    
	reg := regexp.MustCompile(`(.*),(.*),(.*)`)
	src := []byte(" Liu , To prepare , Xuande ")
	dst := []byte(" Personnel information :")
	template := []byte(" surname $1  name $2  word $3")
	match := reg.FindSubmatchIndex(src)
	s := reg.Expand(dst, template, src, match)
	fmt.Printf("%s\n",s)
}
  • result
 Personnel information : Surname Liu   Mingbei   Xuande 

1.2 ExpandString() Method

grammar

func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte

Complete example

  • Code
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
	reg := regexp.MustCompile(`(.*),(.*),(.*)`)
	src := " Liu , To prepare , Xuande "
	dst := []byte(" Personnel information :")
	template := " surname $1  name $2  word $3"
	match := reg.FindStringSubmatchIndex(src)
	s := reg.ExpandString(dst, template, src, match)
	fmt.Printf("%s\n",s)
}
  • Example
 Personnel information : Surname Liu   Mingbei   Xuande 

2. Cut according to regularity

2.1 Split() Method

grammar

func (re *Regexp) Split(s string, n int) []string

explain :

  • n > 0: Split up to a few substrings ( If you cut enough, you won't cut the last one )
  • n == 0: Returns an empty
  • n < 0: Cut as much as you have , Return all results

Complete example

  • Code
package main

import (
	"fmt"
	"regexp"
)

func main() {
    
	myString := "10.10.239.11"
	reg := regexp.MustCompile(`\.`)
	resultList := reg.Split(myString,-1)
	fmt.Printf("%q",resultList)
}
  • result
["10" "10" "239" "11"]

Example (n Results of different values )

package main

import (
	"fmt"
	"regexp"
)

func main() {
    
	myString := "10.10.239.11"
	reg := regexp.MustCompile(`\.`)
	fmt.Printf("n=-1:%q\n",reg.Split(myString,-1))
	fmt.Printf("n=0:%q\n",reg.Split(myString,0))
	fmt.Printf("n=1:%q\n",reg.Split(myString,1))
	fmt.Printf("n=2:%q\n",reg.Split(myString,2))
	fmt.Printf("n=3:%q\n",reg.Split(myString,3))
}
  • result
n=-1["10" "10" "239" "11"]
n=0[]
n=1["10.10.239.11"]
n=2["10" "10.239.11"]
n=3["10" "10" "239.11"]

n=-1, Full cut , And all cutting results are returned .
n=0, Returns an empty .
n=1, Cut into a substring , Equivalent to no cutting
n=2, Cut into two substrings , The first normal cut , The second one is the last one , Don't cut the , Returns all remaining .
n=3, Cut into three substrings , The first two normal cuts , The third one is the last one , Don't cut the , Returns all remaining .

原网站

版权声明
本文为[Development, operation and maintenance Xuande company]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251500403503.html