Golang / 未分类 · 2023年3月7日 0

Golang Note | Regex in golang【】

Quick Start

// Examples From Go by Example
package main

import (
        "bytes"
        "fmt"
        "regexp"
)

func main() {
        pattern := `p(w+)ch`

        match, _ := regexp.MatchString(pattern, "peach")
        fmt.Println(match)
        // Output: true

        // Above we used a string pattern directly, but for other regexp tasks you’ll
        // need to Compile an optimized Regexp struct.
        r, _ := regexp.Compile(pattern)

        fmt.Println(r.MatchString("peach"))
        // Output: true

        fmt.Println(r.FindString("peach punch"))
        // Output: peach

        // This finds the first match but returns the start and end indexes for the
        // match instead of the matching text.
        fmt.Println(r.FindStringIndex("peach punch"))
        // Output: [0 5]

        // The Submatch variants include information about both the whole-pattern
        // matches and the submatches within those matches. For example this will
        // return information for both p([a-z]+)ch and ([a-z]+).
        fmt.Println(r.FindStringSubmatch("peach punch"))
        // Output: [peach ea]

        // Similarly this will return information about the indexes of matches and
        // submatches.
        fmt.Println(r.FindStringSubmatchIndex("peach punch"))
        // Output: [0 5 1 3]

        // The All variants of these functions apply to all matches in the input, not
        // just the first. For example to find all matches for a regexp.
        fmt.Println(r.FindAllString("paaah peach ppp punch aaa pinch pbbbh", -1))
        // Output: [peach punch pinch]

        // These All variants are available for the other functions we saw above as
        // well.
        fmt.Println(r.FindAllStringSubmatchIndex("peach punch pinch", -1))
        // Output: [[0 5 1 3] [6 11 7 9] [12 17 13 15]]

        // Providing a non-negative integer as the second argument to these functions
        // will limit the number of matches.
        fmt.Println(r.FindAllString("peach punch pinch", 2))
        // Output: [peach punch]

        // The regexp package can also be used to replace subsets of strings with
        // other values.
        fmt.Println(r.ReplaceAllString("a peach", ""))
        // Output: a 

        // The Func variant allows you to transform matched text with a given
        // function.
        in := []byte("a peach")
        out := r.ReplaceAllFunc(in, bytes.ToUpper)
        fmt.Println(string(out))
        // Output: a PEACH

}

Basic

High Level

Named group

package main

import (
    "fmt"
    "regexp"
)

var myExp = regexp.MustCompile(`(?Pd+).(d+).(?Pd+)`)

func main() {
    match := myExp.FindStringSubmatch("1234.5678.9")
    result := make(map[string]string)
    for i, name := range myExp.SubexpNames() {
        if i != 0 && name != "" {
            result[name] = match[i]
        }
    }
    fmt.Printf("by name: %s %sn", result["first"], result["second"])

Reference

1. godoc

go doc regexp/syntax | less

2. source code

  • regexp/regexp.go

3. Others

  • regular-expressions-with-go-part-2
  • https://stackoverflow.com/questions/20750843/using-named-matches-from-go-regex
打赏 赞(0) 分享'
分享到...
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

文章目录