当前位置:网站首页>golang正则regexp包使用-05-扩展Expand()、根据正则切割Split()
golang正则regexp包使用-05-扩展Expand()、根据正则切割Split()
2022-06-25 15:32:00 【开发运维玄德公】
1. 扩展
1.1 Expand()方法
语法
func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte
说明
- 将
template的结果添加到dst后边 template中有$1$2……,这些变量可以在src中找到。match是对src对应组的解析。
完整示例
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile(`(.*),(.*),(.*)`)
src := []byte("刘,备,玄德")
dst := []byte("人员信息:")
template := []byte("姓$1 名$2 字$3")
match := reg.FindSubmatchIndex(src)
s := reg.Expand(dst, template, src, match)
fmt.Printf("%s\n",s)
}
- 结果
人员信息:姓刘 名备 字玄德
1.2 ExpandString() 方法
语法
func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte
完整示例
- 代码
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile(`(.*),(.*),(.*)`)
src := "刘,备,玄德"
dst := []byte("人员信息:")
template := "姓$1 名$2 字$3"
match := reg.FindStringSubmatchIndex(src)
s := reg.ExpandString(dst, template, src, match)
fmt.Printf("%s\n",s)
}
- 示例
人员信息:姓刘 名备 字玄德
2. 根据正则切割
2.1 Split() 方法
语法
func (re *Regexp) Split(s string, n int) []string
说明:
- n > 0:最多分割几个子字串(切够了最后一个就不切了)
- n == 0:返回空
- n < 0:有多少切多少,返回所有结果
完整示例
- 代码
package main
import (
"fmt"
"regexp"
)
func main() {
myString := "10.10.239.11"
reg := regexp.MustCompile(`\.`)
resultList := reg.Split(myString,-1)
fmt.Printf("%q",resultList)
}
- 结果
["10" "10" "239" "11"]
示例(n不同值的结果)
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))
}
- 结果
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,完全切割,且返回了所有切割结果。
n=0,返回空。
n=1,切割成一个子字串,相当于没有切割
n=2,切割成两个子字串,第一个正常切,第二个是最后一个了,不切了,返回剩余所有。
n=3,切割成三个子字串,前两个正常切,第三个是最后一个了,不切了,返回剩余所有。
边栏推荐
- A deformation problem of Hanoi Tower
- Advertising effect cluster analysis (kmeans)
- [paper notes] poly yolo: higher speed, more precise detection and instance segmentation for yolov3
- Learning notes on February 5, 2022 (C language)
- 网上办理股票开户安全吗?
- GDB debugging
- Reflection - learning notes
- Learning notes on February 18, 2022 (C language)
- JMeter reading and writing excel requires jxl jar
- Js- get the mouse coordinates and follow them
猜你喜欢

Yolov4 coco pre train Darknet weight file

google_ Breakpad crash detection

Introduction to flexible array

MySQL field truncation principle and source code analysis

Agent and classloader

Design and implementation of timer

Summary of four parameter adjustment methods for machine learning

双目3D感知(一):双目初步认识

If a thread overflows heap memory or stack memory, will other threads continue to work

解决Visio和office365安装兼容问题
随机推荐
0706-- replace fields in the use case, such as mobile phone number or ID
Go build reports an error missing go sum entry for module providing package ... to add:
Day01: learning notes
Es data synchronization mode
Simulating Sir disease transmission model with netlogo
User defined data type - structure
Agent and classloader
Statistical analysis - data level description of descriptive statistics
Afterword of Parl intensive learning 7-day punch in camp
QT pattern prompt box implementation
Pytorch | how to save and load pytorch models?
Boost listening port server
Cross compilation correlation of curl Library
Data feature analysis skills - correlation test
Start using markdown
国信金太阳靠谱吗?是否合法?开股票账户安全吗?
Joseph Ring - formula method (recursive formula)
剑指 Offer II 091. 粉刷房子
Js- get the mouse coordinates and follow them
在打新债开户证券安全吗,需要什么准备