1 回答
TA贡献1851条经验 获得超5个赞
您可以使用以下方式将匹配项从 a[]string转换为 a Set:https ://godoc.org/github.com/golang-collections/collections/set
像这样:
package main
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"regexp"
"strings"
"github.com/golang-collections/collections/set"
)
func main() {
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
p := []string{}
p = append(p, "engineer")
p = append(p, "doctor")
p = append(p, "chemical (permit)")
skills := strings.Join(p, "|")
fmt.Println(skills)
re := regexp.MustCompile(`(?i)` + skills)
for _, file := range files {
if strings.ToLower(filepath.Ext(file.Name())) == ".txt" {
fmt.Println(file.Name())
b, err := ioutil.ReadFile(file.Name()) // just pass the file name
if err != nil {
fmt.Print(err)
}
//fmt.Println(b) // print the content as 'bytes'
str := string(b) // convert content to a 'string'
matches := re.FindAllString(str, -1)
matchesSet := set.New(matches...)
matchesSet.Do(func print(s string) {
fmt.Println(s)
}
// fmt.Println(matches, len(matches))
// for _, j := range matches {
// fmt.Println(j)
// }
}
}
}
- 1 回答
- 0 关注
- 171 浏览
添加回答
举报