Examples of regular expressions in the Go language

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

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

package main
import "bytes"
import "fmt"
import "regexp"
func main() {
// This test 1 Whether the string matches 1 Let's see.
    match, _ := regexp.MatchString("p([a-z]+)ch", "peach")
    fmt.Println(match)
// Up here we're using strings directly, but for 1 Some other regular tasks you need to use Compile 1 An optimized Regexp Structure.
    r, _ := regexp.Compile("p([a-z]+)ch")
// There are many ways to do this structure. Here's something similar to what we saw earlier 1 Two matching tests.
    fmt.Println(r.MatchString("peach"))
// This is looking for a matching string.
    fmt.Println(r.FindString("peach punch"))
// This is also looking for number one 1 Submatched string, but returns the index of the start and end of the match, not the content of the match.
    fmt.Println(r.FindStringIndex("peach punch"))
//Submatch Returns exactly matched and locally matched strings. For example, this is going to return p([a-z]+)ch and `([a-z]+) The information.
    fmt.Println(r.FindStringSubmatch("peach punch"))
// Similarly, this will return exactly matched and locally matched index positions.
    fmt.Println(r.FindStringSubmatchIndex("peach punch"))
// with All This function returns all matches, not just the first match. For example, find all the items that match an expression.
    fmt.Println(r.FindAllString("peach punch pinch", -1))
//All You can also correspond to all the functions up here.
    fmt.Println(r.FindAllStringSubmatchIndex(
        "peach punch pinch", -1))
// This function provides 1 Number of positive integers to limit the number of matches.
    fmt.Println(r.FindAllString("peach punch pinch", 2))
// In the example above, we used the string as a parameter and used the example MatchString This way. We can also provide it []byte Parameter and will String Get rid of function hits.
    fmt.Println(r.Match([]byte("peach")))
// Can be used when creating a regular representation constant Compile A variation of the MustCompile . because Compile Returns two values, not constants.
    r = regexp.MustCompile("p([a-z]+)ch")
    fmt.Println(r)
//regexp Packages can also be used to replace partial strings with other values.
    fmt.Println(r.ReplaceAllString("a peach", "<fruit>"))
//Func Variables allow you to pass matching content to 1 In a given function,
    in := []byte("a peach")
    out := r.ReplaceAllFunc(in, bytes.ToUpper)
    fmt.Println(string(out))
}

Return results:


true
true
peach
[0 5]
[peach ea]
[0 5 1 3]
[peach punch pinch]
[[0 5 1 3] [6 11 7 9] [12 17 13 15]]
[peach punch]
true
p([a-z]+)ch
a <fruit>
a PEACH

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: