V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
yujianwjj
V2EX  ›  Go 编程语言

求一个 go 正则表达式的写法

  •  
  •   yujianwjj · 2022-05-05 18:02:42 +08:00 · 1735 次点击
    这是一个创建于 693 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个目录下有很多 .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")
    			}
    		})
    	}
    }
    
    

    求告知正确的写法。

    第 1 条附言  ·  2022-05-05 19:47:53 +08:00
    解释下为什么不能直接过滤(.1.log )的方式:这个程序是一个日志采集的 agent 。目的根据用户配置的 path (比如用户配置 path: /var/log/.*\.log ),采集对应的文件,另外也不单单是 .1.log ,也有 .2.log 。
    9 条回复    2022-05-06 10:42:58 +08:00
    lozzow
        1
    lozzow  
       2022-05-05 18:20:20 +08:00
    哈哈哈能不能把字符串反转之后不匹配 gol.1.这样的文件呢😁
    ch2
        2
    ch2  
       2022-05-05 18:35:48 +08:00
    字符串反转一下就行了
    AlisaDestiny
        3
    AlisaDestiny  
       2022-05-05 18:37:12 +08:00
    既然标准库不支持 Negative look-ahead 那就找个支持的开源库不就完事了。
    https://github.com/dlclark/regexp2#compare-regexp-and-regexp2
    AX5N
        4
    AX5N  
       2022-05-05 18:59:07 +08:00
    .*([^1]|\..{2,})\.log$
    --------------------------
    当然,用正则表达式来解决这个问题非常蠢,直接截取文件名最后 6 个字符,如果等于 [.1.log] 直接不匹配就行了,两行就能搞定。

    if filename[-6:] == '.1.log':
    continue
    Dvel
        5
    Dvel  
       2022-05-05 19:16:08 +08:00
    这样不就行了吗:
    strings.HasSuffix(filename, ".1.log")
    darklights
        6
    darklights  
       2022-05-05 21:11:59 +08:00
    换个思路。

    如果只有 xx.log xx.2022-04-01.log 这两种形式,那就:
    ^[^.]+(?:\.\d{4}-\d{2}-\d{2})?\.log$

    如果只需排除中间是纯数字,那就:
    ^[^.]+(?:\..*[^\d.]+.*)?\.log$

    以上 ecmascript 正则,不懂 golang
    ropon
        7
    ropon  
       2022-05-06 08:09:47 +08:00 via iPhone
    logagent 采集目录打开的日志文件 lsof +d /path
    eudore
        8
    eudore  
       2022-05-06 09:15:07 +08:00
    `path/filepath.Match` 函数了解一下
    cyjme
        9
    cyjme  
       2022-05-06 10:42:58 +08:00
    用一个支持 look-ahead 的库
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3145 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 12:38 · PVG 20:38 · LAX 05:38 · JFK 08:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.