Golang小例 命令行正则

前端之家收集整理的这篇文章主要介绍了Golang小例 命令行正则前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main
import (
    "bufio"
    "flag"
    "fmt"
    "os"
    "regexp"
)
func main() {
    defer func() {
        return
    }()
    data := flag.String("data","This is a example for test!","Type: Text!")
    length := flag.Bool("len",false,"Type: Bool!")
    r := flag.Bool("r","Type: Bool!")
    reString := flag.String("re","(a)","Type: Text!")
    findCount := flag.Int("count",-1,"Type: Int!")
    first := flag.Int("array",65537,"Type: Int!")
    second := flag.Int("resault","Type: Int!")
    pipe := flag.Bool("pipe","Type: Bool!"

    flag.Parse()

    if *pipe {
        reader := bufio.NewReader(os.Stdin)
        tempData,_,_ := reader.ReadLine()
        *data = string(tempData)
    }

    reg := regexp.MustCompile(*reString)
    resault := reg.FindAllStringSubmatch(*data,*findCount)
    if *length {
        fmt.Println(len(resault))
        return
    }

    if *r {
        for temp := 0; temp < len(resault); temp++ {
            if len(resault[temp]) != 1 {
                for rtemp := 1; rtemp < len(resault[temp]); rtemp++ {
                    fmt.Println(resault[temp][rtemp])
                }
            } else {
                fmt.Println("")
            }
        }
        return
    }


    if *first != 65537 {
        if *second != 65537 {
            fmt.Println(resault[*first][*second])
            return
        }

        fmt.Println(resault[*first])
        return
    }
    fmt.Println(resault)
    return
}

命令行正则筛选 使用命令:
@H_403_7@ @H_403_7@ @H_403_7@ @H_403_7@ @H_403_7@
-data 内容
-re 正则表达式
-count 匹配次数(默认-1全部匹配)
-array 数组选择
-resault 结果选择(1、表达式全部匹配 2、匹配括号内部)
-len
返回结果数目
@H_403_7@
-r
开启递归显示所有结果
-pipe
打开管道

猜你在找的Go相关文章