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
}
命令行正则筛选 使用命令:
-data |
内容 |
-re |
正则表达式 |
-count |
匹配次数(默认-1全部匹配) |
-array |
数组选择 |
-resault |
结果选择(1、表达式全部匹配 2、匹配括号内部) |