doufang1954
doufang1954
2015-08-27 08:35

正则表达式排除不能与inotifywait一起使用的非golang文件

已采纳

Below are the regex for filtering out all the non go files (i.e those with '.go' extension)

^([\S]*[^.][^g][^o]|[\S]*.[^g].|[\S]*..[^o]$|[\S]*[^.]..|[\S]{1,2})$

^([^.]*)($|[.]($|[\S]$|g[^o]$|[^g]o$|[^g][^o]$|([\S]+)\.($|.$|g[^o]$|[^g]o$|[^g][^o]$|[^.]{3,}$)|[^.]{3,}$))

You can test them here (click on try it 'Go' in the menu below the regex)

http://fiddle.re/80kvh6

http://fiddle.re/mhv1h6

While they seem to work correctly in go but not with inotifywait's exclude filter (which uses posix ERE format)

I am trying to setup a "watch and reload" task in my Makefile for a golang project. Also i am assuming file or folders names dont have spaces.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doumao8355 doumao8355 6年前

    You may use

    \.[^.][^.][^.]+$|\.[^.][^o]$|\.[^g][^.]$|\.[^.]$
    

    See demo

    Or if you can group:

    \.([^.][^.][^.]+|[^.][^o]|[^g][^.]|[^.])$
    

    See demo

    See explanation:

    • \. - a literal dot
    • ([^.][^.][^.]+|[^.][^o]|[^g][^.]|[^.]) - a group of alternatives:
      • [^.][^.][^.]+ - 2 characters other than . and 1 or more characters other than ....
      • [^.][^o] - a character other than a dot and a character other than o...
      • [^g][^.] - a character other than g and a character other than a dot...
      • [^.] - a character other than a dot...
    • $ - right before the end of string.
    点赞 评论 复制链接分享
  • dp6319 dp6319 6年前

    Got the issue. It seems [:graph:] not \S the class identifier for non-space chars in extended posix. The below is good to go.

    ^[[:graph:]]*[^.][^g][^o]$|^[[:graph:]]{1,2}$|^[[:graph:]]*.[^g].$|^[[:graph:]]*..[^o]$|^[[:graph:]]*[^.]..$

    or even a better one from stribizhev comment [^.][^g][^o]$|^..$|.[^g].$|..[^o]$|[^.]..$

    点赞 评论 复制链接分享

相关推荐