package main import ( "github.com/tealeg/xlsx" "regexp" "strings" ) type TagMatching struct { matchKey string matchKeyWay []string // title、detail、projectname、filetext matchKeyReg []*RegexpInfo addKey string addKeyWay []string addKeyReg []*RegexpInfo excludeKey string //排除词 excludeKeyWay []string //排除词匹配方式 excludeKeyReg []*RegexpInfo } type RegexpInfo struct { keyStr string regs *regexp.Regexp } var MatchWayMap = map[string]string{ "标题匹配": "title", "全文匹配": "detail", "附件匹配": "filetext", "项目名称匹配": "projectname", "采购单位行业": "buyerclass", } func initExcel(path string) { xlFile, err := xlsx.OpenFile(path) if err != nil { return } for _, sheet := range xlFile.Sheet { tagArr = append(tagArr, sheet.Name) var rules []TagMatching for rowIndex, row := range sheet.Rows { if rowIndex == 0 { continue } tag := TagMatching{} for cellIndex, cell := range row.Cells { if cell.String() != "" { if cellIndex == 0 { tag.matchKey = cell.String() tag.matchKeyReg = initRegex(cell.String()) } else if cellIndex == 1 { tag.matchKeyWay = strings.Split(cell.String(), ",") } else if cellIndex == 2 { tag.addKey = cell.String() tag.addKeyReg = initRegex(cell.String()) } else if cellIndex == 3 { tag.addKeyWay = strings.Split(cell.String(), ",") } else if cellIndex == 4 { tag.excludeKey = cell.String() tag.excludeKeyReg = initRegex(cell.String()) } else if cellIndex == 5 { tag.excludeKeyWay = strings.Split(cell.String(), ",") } } } rules = append(rules, tag) } TagMatchRule[sheet.Name] = rules } } func initRegex(key string) []*RegexpInfo { var infos []*RegexpInfo if strings.Contains(key, ",") { for _, s := range strings.Split(key, ",") { if strings.Contains(s, "&&") { info := &RegexpInfo{ keyStr: s, regs: nil, } infos = append(infos, info) } else { info := &RegexpInfo{ keyStr: s, regs: regexp.MustCompile(".*(?i)" + s + ".*"), } infos = append(infos, info) } } } else { info := &RegexpInfo{ keyStr: key, regs: regexp.MustCompile(".*(?i)" + key + ".*"), } infos = append(infos, info) } return infos }