当前位置:网站首页>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

Method Replace target string type Replace source string type Return value Return type
ReplaceAll()[ ]byte[ ]byte Returns the replaced string [ ]byte
ReplaceAllString()stringstring Returns the replaced string string
ReplaceAllLiteral()[ ]byte[ ]byte Returns the replaced string [ ]byte
ReplaceAllLiteralString()stringstring Returns the replaced string string
ReplaceAllFunc()[ ]bytefunc() Returns the replaced string [ ]byte
ReplaceAllStringFunc()stringfunc() 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
原网站

版权声明
本文为[Development, operation and maintenance Xuande company]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230446280081.html