当前位置:网站首页>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,切割成三个子字串,前两个正常切,第三个是最后一个了,不切了,返回剩余所有。
边栏推荐
- Record the time to read the file (the system cannot find the specified path)
- Common dynamic memory errors
- 解决Visio和office365安装兼容问题
- Yolov5 Lite: fewer parameters, higher accuracy and faster detection speed
- Esp8266 building smart home system
- Solve valueerror: invalid literal for int() with base 10
- iconv_ Open returns error code 22
- Leetcode123 timing of buying and selling stocks III
- Golang channel reading data
- Why should the coroutine be set to non blocking IO
猜你喜欢

Paddlepaddle paper reproduction course biggan learning experience

1090.Phone List

Data preprocessing - normalization and standardization

Distributed transaction solution

Shared memory synchronous encapsulation

Js- get the mouse coordinates and follow them

JSON module dictionary and string conversion

Pytorch distributed test pit summary

Brief object memory layout
![[paper notes] mcunetv2: memory efficient patch based influence for tiny deep learning](/img/4b/f446bd37057237c0ba4c7b4e38e74f.jpg)
[paper notes] mcunetv2: memory efficient patch based influence for tiny deep learning
随机推荐
网上办理股票开户安全吗?
Principle and implementation of MySQL master-slave replication (docker Implementation)
Efficient pytorch: how to eliminate training bottlenecks
JSON module dictionary and string conversion
Websocket (WS) cluster solution
JVM memory region details
(translation) json-rpc 2.0 specification (Chinese version)
Kali SSH Remote Login
System Verilog - data type
Summary of four parameter adjustment methods for machine learning
JS capture, target, bubble phase
Pytorch | how to save and load pytorch models?
The last glory of the late Ming Dynasty - the battle of Korea
5 connection modes of QT signal slot
Install Kali extension 1: (kali resolution problem)
Summary of common methods of ArrayList, LinkedList and vector, and analysis of source code learning
Common dynamic memory errors
(1) Introduction
MySQL transaction characteristics and implementation principle
Generation method and usage of coredump