Go language regular expression usage example summary [find match replace etc.]

  • 2020-06-01 10:02:24
  • OfStack

This article illustrates the use of regular expressions in the Go language. I will share it with you for your reference as follows:

The use of regular expressions in the Go language is simple, with sample code:

package test
import (
    "fmt"
    "regexp"
)
func RegixBase() {
    //findTest()
    //findIndexTest()
    //findStringTest()
    //findChinesString()
    //findNumOrLowerLetter()
    findAndReplace()
}
// The incoming []byte To return to []byte
func findTest() {
    str := "ab001234hah120210a880218end"
    reg := regexp.MustCompile("\\d{6}") //6 A continuous digit
    fmt.Println("------Find------")
    // return str In the first 1 A match reg The string
    data := reg.Find([]byte(str))
    fmt.Println(string(data))
    fmt.Println("------FindAll------")
    // return str All matches in reg The string
    // The first 2 The maximum number of parameters to be returned is passed -1 Returns all results
    dataSlice := reg.FindAll([]byte(str), -1)
    for _, v := range dataSlice {
        fmt.Println(string(v))
    }
}
// The incoming []byte , returns the index of the first and last position
func findIndexTest() {
    fmt.Println("------FindIndex------")
    // Returns the first 1 The first and last position of a matched string
    reg2 := regexp.MustCompile("start\\d*end") //start To start, end It's over. It's all Numbers in the middle
    str2 := "00start123endhahastart120PSend09start10000end"
    //index[0] Is the starting position, index[1] Is the end position
    index := reg2.FindIndex([]byte(str2))
    fmt.Println("start:", index[0], ",end:", index[1], str2[index[0]:index[1]])
    fmt.Println("------FindAllIndex------")
    // Returns the first and last positions of all matched strings
    indexSlice := reg2.FindAllIndex([]byte(str2), -1)
    for _, v := range indexSlice {
        fmt.Println("start:", v[0], ",end:", v[1], str2[v[0]:v[1]])
    }
}
// The incoming string To return to string (more convenient)
func findStringTest() {
    fmt.Println("------FindString------")
    str := "ab001234hah120210a880218end"
    reg := regexp.MustCompile("\\d{6}") //6 A continuous digit
    fmt.Println(reg.FindString(str))
    fmt.Println(reg.FindAllString(str, -1))
    // The following two methods are similar
    fmt.Println(reg.FindStringIndex(str))
    fmt.Println(reg.FindIndex([]byte(str)))
}
// To find the Chinese characters
func findChinesString() {
    str := "hello China hello The peace of the world hi good "
    reg := regexp.MustCompile("[\\p{Han}]+")
    fmt.Println(reg.FindAllString(str, -1))
    //[ China The peace of the world good ]
}
// Look for Numbers or lowercase letters
func findNumOrLowerLetter() {
    str := "HAHA00azBAPabc09FGabHY99"
    reg := regexp.MustCompile("[\\d|a-z]+")
    fmt.Println(reg.FindAllString(str, -1))
    //[00az abc09 ab 99]
}
// Find and replace
func findAndReplace() {
    str := "Welcome for Beijing-Tianjin CRH train."
    reg := regexp.MustCompile(" ")
    fmt.Println(reg.ReplaceAllString(str, "@")) // Replace the space with @ character
    //Welcome@for@Beijing-Tianjin@CRH@train.
}

PS: here are two more handy regular expression tools for you to use:

JavaScript regular expression online testing tool:
http://tools.ofstack.com/regex/javascript

Online regular expression generation tool:
http://tools.ofstack.com/regex/create_reg

I hope this article has been helpful to you in the programming of Go language.


Related articles: