当前位置:网站首页>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
边栏推荐
- exe闪退的原因查找方法
- Remove duplicates from sorted list II of leetcode topic resolution
- Kotlin collaboration +retro most elegant network request use
- Redis 哨兵
- 密码学系列之:PKI的证书格式表示X.509
- 内存分析与内存泄漏检测
- How to add libraries for Arduino ide installation
- Wechat tried out the 1065 working system, and was forced to leave work at 18:00; It is said that Apple will no longer develop off screen fingerprint identification; Amd chief independent GPU architect
- 【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)
- 使用aggregation API扩展你的kubernetes API
猜你喜欢

图解 Google V8 # 17:消息队列:V8是怎么实现回调函数的?

Day_11 传智健康项目-图形报表、POI报表

Gplearn appears assignment destination is read only

【Leetcode】431. Encode n-ary tree to binary tree (difficult)

Layer 2技术方案进展情况

Basic RF theory (DB)

Day_10 传智健康项目-权限控制、图形报表

mongodb 4.x绑定多个ip启动报错

内存分析与内存泄漏检测

【Leetcode】431. Encode N-ary Tree to Binary Tree(困难)
随机推荐
Perfect squares for leetcode topic analysis
ant使用总结(二):相关命令说明
Ant Usage Summary (II): description of related commands
程序员的真实想法 | 每日趣闻
【Cocos2d-x】截图分享功能
Pat class B 1022 d-ary a+b
(1)基础学习——vim编辑器常用快捷操作命令
Kotlin interface
[DaVinci developer topic] -41-app how SWC reads and writes NVM block data
ant使用总结(一):使用ant自动打包apk
Day_04 传智健康项目-预约管理-套餐管理
如何为 Arduino IDE 安装添加库
Pyqt5 setting window top left Icon
[focus on growth and build a dream for the future] - TDP year-end event, three chapters go to the Spring Festival!
又到半年总结时,IT人只想躺平
内存分析与内存泄漏检测
Pat class B 1021 digit statistics
Sorting out common problems after crawler deployment
Possible pits in mongodb project
Day_ 13 smart health project - Chapter 13