一个目录下有很多 .log 的文件
xx.log
xx.2022-04-01.log
xx.1.log
需要排除 .1.log 这样的文件。
由于 go 不支持 Negative look-ahead 。不能用 (?!
func TestRegex2(t *testing.T) {
r, err := regexp.Compile(`.*([^.]\D)\.log$`)
if err != nil {
t.Fatal(err)
}
cases := []struct {
dest string
match bool
}{
{"/afafa/a.log", true},
{"/afafa/a_err.log", true},
{"/afafa/a_.log", true},
{"/afafa/a_3.log", true}, // 报错
{"/afafa/a_3.abc.log", true},
{"/afafa/a.1.log", false},
{"/afafa/a.2022.log", true}, // 报错
{"/afafa/a.2022-4-1.log", true}, // 报错
}
for i := range cases {
t.Run(cases[i].dest, func(t *testing.T) {
if r.MatchString(cases[i].dest) != cases[i].match {
t.Errorf("match not correct")
}
})
}
}
求告知正确的写法。