当前位置:网站首页>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
templateThe results of are added todstBack templateThere is$1$2……, These variables can be insrcFind .matchIt's rightsrcResolution 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 .
边栏推荐
- QT source code online view
- 程序员 VS 黑客的思维 | 每日趣闻
- 1090.Phone List
- System Verilog - thread
- Weka download and installation
- Is it safe to open a stock account through the account opening link given by the account manager? I want to open an account
- Detailed summary of reasons why alertmanager fails to send alarm messages at specified intervals / irregularly
- 剑指 Offer 05. 替换空格
- Paddlepaddle paper reproduction course biggan learning experience
- Data preprocessing - normalization and standardization
猜你喜欢

QT source code online view

Custom structure type
![[paper notes] street view change detection with deconvolutional networks](/img/2d/777fd0d85ff4d349516b95923410fd.jpg)
[paper notes] street view change detection with deconvolutional networks

Simulating Sir disease transmission model with netlogo

Single user mode
![[paper notes] contextual transformer networks for visual recognition](/img/e4/45185983e28664564bbf79023ccbf6.jpg)
[paper notes] contextual transformer networks for visual recognition

程序员 VS 黑客的思维 | 每日趣闻

Record the time to read the file (the system cannot find the specified path)

Using R language in jupyter notebook

Highly concurrent optimized Lua + openresty+redis +mysql (multi-level cache implementation) + current limit +canal synchronization solution
随机推荐
MySQL修改字段语句
Distributed transaction solution
Is it safe to open a stock account online?
Leetcode121 timing of buying and selling stocks
Generation method and usage of coredump
客户经理给的开户链接办理股票开户安全吗?我想开个户
Completabilefuture of asynchronous tools for concurrent programming
Start using markdown
剑指 Offer 06. 从尾到头打印链表
剑指 Offer 10- I. 斐波那契数列
Is Guoxin golden sun reliable? Is it legal? Is it safe to open a stock account?
Brief introduction to class loading process
How to deal with mining process
Solution of push code failure in idea
MySQL field truncation principle and source code analysis
About%*s and%* s
golang reverse a slice
通过客户经理的开户链接开股票账户安全吗?
Internal class learning notes
1090.Phone List