当前位置:网站首页>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
边栏推荐
- Remove duplicates from sorted list II of leetcode topic resolution
- 原址 交换
- 金融科技之高效办公(一):自动生成信托计划说明书
- Day_ 06 smart health project - mobile terminal development - physical examination appointment
- Runc symbolic link mount and container escape vulnerability alert (cve-2021-30465)
- Summary of ant usage (I): using ant to automatically package apk
- Visual studio debugging tips
- mysql以逗号分隔的字段作为查询条件怎么查——find_in_set()函数
- (1) Basic learning - Common shortcut commands of vim editor
- Find the number of nodes in the widest layer of a binary tree
猜你喜欢

Microsoft interview question: creases in origami printing

Memory analysis and memory leak detection

Day_12 传智健康项目-JasperReports

Learning Tai Chi Maker - esp8226 (11) distribution network with WiFi manager Library
![[vivado] xilinxcedstore introduction](/img/c7/4f203d125ddb18378398a7eaeffaf5.png)
[vivado] xilinxcedstore introduction

又到半年总结时,IT人只想躺平

Day_04 传智健康项目-预约管理-套餐管理

Day_ 07 smart communication health project FreeMarker

Progress of layer 2 technical scheme

How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function
随机推荐
Dora's Google SEO tutorial (1) SEO novice guide: establishment of preliminary optimization thinking
mongodb 4.x绑定多个ip启动报错
Day_13 传智健康项目-第13章
Matplotlib savefig multiple picture overlay
WordPress contact form entries cross cross site scripting attack
SSM project construction
射频基础理论(dB)
【已解决】“The Unity environment took too long to respond. Make sure that :\n“
Design scheme of Small PLC based on t5l1
Day_ 05 smart communication health project - appointment management - appointment settings
去除防火墙和虚拟机对live555启动IP地址的影响
Efficient office of fintech (I): automatic generation of trust plan specification
Microsoft interview question: creases in origami printing
Ansible uses ordinary users to manage the controlled end
十一、纺织面料下架功能的实现
Layer 2技术方案进展情况
[cocos2d-x] erasable layer:erasablelayer
又到半年总结时,IT人只想躺平
exe闪退的原因查找方法
Day_ 02 smart communication health project - appointment management - inspection item management