当前位置:网站首页>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
边栏推荐
- 使用aggregation API扩展你的kubernetes API
- 射频内容学习
- Day_11 传智健康项目-图形报表、POI报表
- Long substring without repeating characters for leetcode topic resolution
- There are so many code comments! I laughed
- Matplotlib savefig multiple picture overlay
- Gplearn appears assignment destination is read only
- 基于T5L1的小型PLC设计方案
- JVM原理简介
- golang正则regexp包使用-04-使用正则替换(ReplaceAll(),ReplaceAllLiteral(),ReplaceAllFunc())
猜你喜欢

11、 Realization of textile fabric off shelf function

Microsoft interview question: creases in origami printing

Day_ 01 smart communication health project - project overview and environmental construction

射频内容学习

【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)

【Cocos2d-x】自定义环形菜单

Ant Usage Summary (II): description of related commands

Cloud native database is the future

(1)基础学习——vim编辑器常用快捷操作命令

Infotnews | which Postcard will you receive from the universe?
随机推荐
mysql读已提交和可重复度区别
How to batch produce QR codes that can be read online after scanning
11、 Realization of textile fabric off shelf function
金融科技之高效办公(一):自动生成信托计划说明书
Day_07 传智健康项目-Freemarker
射频内容学习
Jour 04 projet de santé mentale - gestion des rendez - vous - gestion des forfaits
SQL表名与函数名相同导致SQL语句错误。
WordPress Core 5.8.2 - 'WP_ Query'SQL injection
Memory analysis and memory leak detection
sklearn sklearn中的模型调参利器 gridSearchCV(网格搜索)
机器学习3-岭回归,Lasso,变量选择技术
Pyqt5 setting window top left Icon
Day_ 03 smart communication health project - appointment management - inspection team management
Dora's Google SEO tutorial (1) SEO novice guide: establishment of preliminary optimization thinking
How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function
(1) Basic learning - Common shortcut commands of vim editor
Pat class B 1024 scientific notation C language
Radar canvas
使用aggregation API扩展你的kubernetes API