golang正则regexp包使用-04-使用正则替换(ReplaceAll(),ReplaceAllLiteral(),ReplaceAllFunc())

方法替换目标字串类型替换源字串类型返回值返回类型
ReplaceAll()[ ]byte[ ]byte返回被替换后的字串[ ]byte
ReplaceAllString()stringstring返回被替换后的字串string
ReplaceAllLiteral()[ ]byte[ ]byte返回被替换后的字串[ ]byte
ReplaceAllLiteralString()stringstring返回被替换后的字串string
ReplaceAllFunc()[ ]bytefunc()返回被替换后的字串[ ]byte
ReplaceAllStringFunc()stringfunc()返回被替换后的字串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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值