当前位置:网站首页>golang正则regexp包使用-04-使用正则替换(ReplaceAll(),ReplaceAllLiteral(),ReplaceAllFunc())
golang正则regexp包使用-04-使用正则替换(ReplaceAll(),ReplaceAllLiteral(),ReplaceAllFunc())
2022-06-23 04:46:00 【开发运维玄德公】
文章目录
| 方法 | 替换目标字串类型 | 替换源字串类型 | 返回值 | 返回类型 |
|---|---|---|---|---|
| ReplaceAll() | [ ]byte | [ ]byte | 返回被替换后的字串 | [ ]byte |
| ReplaceAllString() | string | string | 返回被替换后的字串 | string |
| ReplaceAllLiteral() | [ ]byte | [ ]byte | 返回被替换后的字串 | [ ]byte |
| ReplaceAllLiteralString() | string | string | 返回被替换后的字串 | string |
| ReplaceAllFunc() | [ ]byte | func() | 返回被替换后的字串 | [ ]byte |
| ReplaceAllStringFunc() | string | func() | 返回被替换后的字串 | string |
1. 正则替换
1.1 ReplaceAll() 方法
语法
func (re *Regexp) ReplaceAll(src []byte, repl []byte) []byte
完整示例
- 代码
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("原字串:%q\n替换后:%q",myString,s)
}
- 结果
原字串:"10.10.239.11"
替换后:"10.10.239.0/16"
示例(使用分组 1)
- 代码
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("原字串:%q\n替换后:%q",myString,s)
}
- 结果
原字串:"10.10.239.11"
替换后:"10.10.239.0/16"
示例(使用分组 2)
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("(.)(.)(.)(.)(.)(.)(.)")
myString := "柳庭风静人眠昼"
rep := []byte("${7}${6}${5}${4}${3}${2}${1}")
s := reg.ReplaceAll([]byte(myString),rep)
fmt.Printf("原字串:%q\n替换后:%q",myString,s)
}
- 结果
原字串:"柳庭风静人眠昼"
替换后:"昼眠人静风庭柳"
1.2 ReplaceAllString()
语法
func (re *Regexp) ReplaceAllString(src string, repl string) string
完整示例
- 代码
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("原字串:%q\n替换后:%q",myString,s)
}
- 结果
原字串:"10.10.239.11"
替换后:"10.10.239.0/16"
2. 按原文替换
2.1 ReplaceAllLiteral()
“按原文的”说明rep中会按原文替换,即rep中的分组不会生效(我们将在“示例(按原文替换)”中演示。)
语法
func (re *Regexp) ReplaceAllLiteral(src []byte, repl []byte) []byte
完整示例
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("原字串:%q\n替换后:%q",myString,s)
}
- 显示结果
原字串:"10.10.239.11"
替换后:"10.10.239.0/16"
示例(按原文替换)
- 代码
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("原字串:%q\n替换后:%q",myString,s)
}
- 结果
原字串:"10.10.239.11"
替换后:"${1}0/16"
如上可见,repl中的
${1}被原封不动的按字串替换了。
2.2 ReplaceAllLiteralString()
和ReplaceAllLiteral一样,repl中不可以使用分组的变量
语法
func (re *Regexp) ReplaceAllLiteralString(src string, repl string) string
完整示例
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("原字串:%q\n替换后:%q",myString,s)
}
- 结果
原字串:"10.10.239.11"
替换后:"10.10.239.0/16"
3. 函数处理替换源字串
3.1 ReplaceAllFunc()
语法
func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte
ReplaceAllFunc()方法,初始化实例已经包含了正则,只需要传入:原字串(src)、要替换的字串(repl)。
repl是一个函数,传入值是正则匹配到的字串,传出一个经该函数处理过的值。
完整示例
- 代码
package main
import (
"fmt"
"regexp"
)
func main() {
reg := regexp.MustCompile("\\w+$")
myString := "www.xishu.com"
result := reg.ReplaceAllFunc([]byte(myString),getRepl)
fmt.Printf("最终替换结果:%s\n",result )
}
func getRepl(match []byte) []byte {
var rspl []byte
fmt.Printf("正则匹配结果:%s\n",match)
rspl = append(rspl,match...)
rspl = append(rspl,".cn"...)
return rspl
}
- 结果
正则匹配结果:com
最终替换结果:www.xishu.com.cn
3.2 ReplaceAllStringFunc()
语法
func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string
完整示例
- 代码
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("最终替换结果:%s\n",result )
}
func getRepl(match string) string {
var rspl string
fmt.Printf("正则匹配结果:%s\n",match)
rspl = match + ".cn"
return rspl
}
- 结果
正则匹配结果:com
最终替换结果:www.xishu.com.cn
边栏推荐
- Day_05 传智健康项目-预约管理-预约设置
- 【Cocos2d-x】自定义环形菜单
- Cloud native database is the future
- [DaVinci developer topic] -41-app how SWC reads and writes NVM block data
- Memory analysis and memory leak detection
- Pat class B 1015 C language
- Fraction to recursing decimal
- [cocos2d-x] screenshot sharing function
- Pyinstaller sklearn reports errors
- Leetcode topic resolution remove nth node from end of list
猜你喜欢

sklearn sklearn中classification_report&精确度/召回率/F1值

Efficient office of fintech (I): automatic generation of trust plan specification

How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function

Design scheme of Small PLC based on t5l1

射频基础理论(dB)

Day_ 03 smart communication health project - appointment management - inspection team management

Day_07 传智健康项目-Freemarker

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

Day_13 傳智健康項目-第13章

mongodb 4.x绑定多个ip启动报错
随机推荐
Perfect squares for leetcode topic analysis
[cocos2d-x] custom ring menu
Possible pits in mongodb project
Add and multiply two polynomials using linked list
Visual studio debugging tips
Matplotlib savefig multiple picture overlay
Day_03 传智健康项目-预约管理-检查组管理
Cryptography series: certificate format representation of PKI X.509
Pat class B 1016 C language
Day_ 05 smart communication health project - appointment management - appointment settings
Pat class B 1026 program running time
Redis 哨兵
gplearn出现 assignment destination is read-only
Paper notes: multi label learning lsml
ant使用总结(一):使用ant自动打包apk
Day_13 传智健康项目-第13章
Day_12 传智健康项目-JasperReports
sklearn sklearn中classification_report&精确度/召回率/F1值
Summary of ant usage (I): using ant to automatically package apk
Microsoft interview question: creases in origami printing