這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package mainimport ( "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", false, "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", 65537, "Type: Int!") pipe := flag.Bool("pipe", false, "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 |
Regex |
-count |
匹配次數(預設-1全部匹配) |
-array |
數組選擇 |
-resault |
結果選擇(1、運算式全部匹配 2、匹配括弧內部) |