当前位置:网站首页>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】
List of articles
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 todst
Back template
There is$1
$2
……, These variables can be insrc
Find .match
It's rightsrc
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 .
边栏推荐
- Breakpad usage and DMP analysis
- 剑指 Offer 10- I. 斐波那契数列
- iconv_ Open returns error code 22
- Sampling method and descriptive statistical function in R language
- Is it safe to open a stock account through the account opening link of the account manager?
- MySQL transaction characteristics and implementation principle
- How to package rpm
- Generation method and usage of coredump
- Yolov4 coco pre train Darknet weight file
- Kali SSH Remote Login
猜你喜欢
QT excel table read / write library - qtxlsx
Graphic control and layout basis of R visualization
Arthas source code learning-1
Fishing detection software
解决Visio和office365安装兼容问题
Day01: learning notes
Several relationships of UML
[paper notes] mcunetv2: memory efficient patch based influence for tiny deep learning
Solution of push code failure in idea
04. binary tree
随机推荐
Errno perrno and strerrno
Distributed transaction solution
Afterword of Parl intensive learning 7-day punch in camp
双目3D感知(一):双目初步认识
通过客户经理的开户链接开股票账户安全吗?
Kali modify IP address
Breakpad usage and DMP analysis
剑指 Offer 05. 替换空格
System Verilog - function and task
Leetcode122 timing of buying and selling stocks II
The last glory of the late Ming Dynasty - the battle of Korea
Fishing detection software
Semaphore function
Statistical analysis - data level description of descriptive statistics
QT source code online view
Error com mysql. cj. jdbc. exceptions. Communicationsexception: solutions to communications link failure
[paper notes] rethinking and improving relative position encoding for vision transformer
Summary of regularization methods
Reflection - learning notes
About?: Notes for