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", "<fruit>"))
        // Output: a <fruit>

        // 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(`(?P<first>\d+)\.(\d+).(?P<second>\d+)`)

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 %s\n", result["first"], result["second"])

Reference

1. godoc

go doc regexp/syntax | less

2. source code

3. Others

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容