04-使用正则替换

方法 替换目标字串类型 替换源字串类型 返回值 返回类型
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

posted on   运维开发玄德公  阅读(21)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示