当前位置:网站首页>Golang regular regexp package use -04- use regular replacement (replaceall(), replaceallliteral(), replaceallfunc())
Golang regular regexp package use -04- use regular replacement (replaceall(), replaceallliteral(), replaceallfunc())
2022-06-23 06:27:00 【Development, operation and maintenance Xuande company】
List of articles
| Method | Replace target string type | Replace source string type | Return value | Return type |
|---|---|---|---|---|
| ReplaceAll() | [ ]byte | [ ]byte | Returns the replaced string | [ ]byte |
| ReplaceAllString() | string | string | Returns the replaced string | string |
| ReplaceAllLiteral() | [ ]byte | [ ]byte | Returns the replaced string | [ ]byte |
| ReplaceAllLiteralString() | string | string | Returns the replaced string | string |
| ReplaceAllFunc() | [ ]byte | func() | Returns the replaced string | [ ]byte |
| ReplaceAllStringFunc() | string | func() | Returns the replaced string | string |
1. Regular substitution
1.1 ReplaceAll() Method
grammar
func (re *Regexp) ReplaceAll(src []byte, repl []byte) []byte
Complete example
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\d+$")
myString := "10.10.239.11"
repl := "0/16"
s := reg.ReplaceAll([]byte(myString),[]byte(repl))
fmt.Printf(" Original string :%q\n After replacement :%q",myString,s)
}
- result
Original string :"10.10.239.11"
After replacement :"10.10.239.0/16"
Example ( Usage grouping 1)
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("(\\d.*\\.)\\d+")
myString := "10.10.239.11"
repl := "${1}0/16"
s := reg.ReplaceAll([]byte(myString),[]byte(repl))
fmt.Printf(" Original string :%q\n After replacement :%q",myString,s)
}
- result
Original string :"10.10.239.11"
After replacement :"10.10.239.0/16"
Example ( Usage grouping 2)
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("(.)(.)(.)(.)(.)(.)(.)")
myString := " Liu Tingfeng sleeps in the daytime "
rep := []byte("${7}${6}${5}${4}${3}${2}${1}")
s := reg.ReplaceAll([]byte(myString),rep)
fmt.Printf(" Original string :%q\n After replacement :%q",myString,s)
}
- result
Original string :" Liu Tingfeng sleeps in the daytime "
After replacement :" Sleeping in the daytime, the wind is still and the willows are in the courtyard "
1.2 ReplaceAllString()
grammar
func (re *Regexp) ReplaceAllString(src string, repl string) string
Complete example
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\d+$")
myString := "10.10.239.11"
repl := "0/16"
s := reg.ReplaceAllString(myString,repl)
fmt.Printf(" Original string :%q\n After replacement :%q",myString,s)
}
- result
Original string :"10.10.239.11"
After replacement :"10.10.239.0/16"
2. Replace as original
2.1 ReplaceAllLiteral()
“ According to the original ” explain rep Will be replaced by the original text , namely rep Groups in will not take effect ( We will be in “ Example ( Replace as original )” Demo in .)
grammar
func (re *Regexp) ReplaceAllLiteral(src []byte, repl []byte) []byte
Complete example
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\d+$")
myString := "10.10.239.11"
repl := "0/16"
s := reg.ReplaceAllLiteral([]byte(myString),[]byte(repl))
fmt.Printf(" Original string :%q\n After replacement :%q",myString,s)
}
- Show results
Original string :"10.10.239.11"
After replacement :"10.10.239.0/16"
Example ( Replace as original )
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("(\\d.*\\.)\\d+")
myString := "10.10.239.11"
repl := "${1}0/16"
s := reg.ReplaceAllLiteral([]byte(myString),[]byte(repl))
fmt.Printf(" Original string :%q\n After replacement :%q",myString,s)
}
- result
Original string :"10.10.239.11"
After replacement :"${1}0/16"
As can be seen above ,repl Medium
${1}Replaced by the original string .
2.2 ReplaceAllLiteralString()
and ReplaceAllLiteral equally ,repl Grouped variables cannot be used in
grammar
func (re *Regexp) ReplaceAllLiteralString(src string, repl string) string
Complete example
package main
import (
"fmt"
"regexp"
)
func main() {
//reg := regexp.MustCompile("(\\d.*\\.)\\d+")
reg := regexp.MustCompile("\\d+$")
myString := "10.10.239.11"
repl := "0/16"
s := reg.ReplaceAllLiteralString(myString,repl)
fmt.Printf(" Original string :%q\n After replacement :%q",myString,s)
}
- result
Original string :"10.10.239.11"
After replacement :"10.10.239.0/16"
3. Function to handle the replacement source string
3.1 ReplaceAllFunc()
grammar
func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
ReplaceAllFunc() Method , The initialization instance already contains regular , Just pass in : Original string (src)、 The string to replace (repl).
repl It's a function , The incoming value is a regular matching string , Pass out a value processed by this function .
Complete example
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\w+$")
myString := "www.xishu.com"
result := reg.ReplaceAllFunc([]byte(myString),getRepl)
fmt.Printf(" The final replacement result :%s\n",result )
}
func getRepl(match []byte) []byte {
var rspl []byte
fmt.Printf(" Regular matching results :%s\n",match)
rspl = append(rspl,match...)
rspl = append(rspl,".cn"...)
return rspl
}
- result
Regular matching results :com
The final replacement result :www.xishu.com.cn
3.2 ReplaceAllStringFunc()
grammar
func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string
Complete example
- Code
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\w+$")
myString := "www.xishu.com"
//repl := "0/16
result := reg.ReplaceAllStringFunc(myString,getRepl)
fmt.Printf(" The final replacement result :%s\n",result )
}
func getRepl(match string) string {
var rspl string
fmt.Printf(" Regular matching results :%s\n",match)
rspl = match + ".cn"
return rspl
}
- result
Regular matching results :com
The final replacement result :www.xishu.com.cn
边栏推荐
- Kotlin interface
- Runc symbolic link mount and container escape vulnerability alert (cve-2021-30465)
- Difference between MySQL read committed and repeatability
- mongodb 4. X binding multiple IP startup errors
- 密码学系列之:PKI的证书格式表示X.509
- Day_ 05 smart communication health project - appointment management - appointment settings
- [cocos2d-x] erasable layer:erasablelayer
- Leetcode topic resolution valid Sudoku
- pyinstaller 打包exe设置图标不显示
- Day_ 08 smart health project - mobile terminal development - physical examination appointment
猜你喜欢

Docker实战 -- 部署Redis集群与部署微服务项目

Day_ 05 smart communication health project - appointment management - appointment settings

Day_ 09 smart health project - mobile terminal development - Mobile quick login and permission control

Design scheme of Small PLC based on t5l1

qt creater搭建osgearth环境(osgQT MSVC2017)

Radar canvas

Day_05 传智健康项目-预约管理-预约设置

Day_13 傳智健康項目-第13章

Redis sentry

sklearn sklearn中的模型调参利器 gridSearchCV(网格搜索)
随机推荐
SSM project construction
Ansible uses ordinary users to manage the controlled end
Day_08 传智健康项目-移动端开发-体检预约
What is a PDCA cycle? How to integrate PDCA cycle and OKR
Long substring without repeating characters for leetcode topic resolution
Day_05 传智健康项目-预约管理-预约设置
sklearn sklearn中classification_report&精确度/召回率/F1值
Infotnews | which Postcard will you receive from the universe?
熟练利用切片操作
Ant Usage Summary (II): description of related commands
Design scheme of Small PLC based on t5l1
JVM原理简介
mongodb 4. X binding multiple IP startup errors
图解 Google V8 # 17:消息队列:V8是怎么实现回调函数的?
Remove the influence of firewall and virtual machine on live555 startup IP address
Day_ 05 smart communication health project - appointment management - appointment settings
Leetcode topic resolution valid anagram
Layer 2技术方案进展情况
Shutter style
Day_02 传智健康项目-预约管理-检查项管理