Golang Note | Regex in golang

WangLane · · 281 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:WangLane

查看原文:Golang Note | Regex in golang

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

281 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传