regular expression to exclude filetypes from find

ぐ巨炮叔叔 提交于 2019-12-18 14:09:17

问题


When using find command in linux, one can add a -regex flag that uses emacs regualr expressions to match.

I want find to look for all files except .jar files and .ear files. what would be the regular expression in this case?

Thanks


回答1:


You don't need a regex here. You can use find with the -name and -not options:

find . -not -name "*.jar" -not -name "*.ear"

A more concise (but less readable) version of the above is:

find . ! \( -name "*.jar" -o -name "*.ear" \)



回答2:


EDIT: New approach:

Since POSIX regexes don't support lookaround, you need to negate the match result:

find . -not -regex ".*\.[je]ar"

The previously posted answer uses lookbehind and thus won't work here, but here it is for completeness' sake:

.*(?<!\.[je]ar)$



回答3:


find . -regextype posix-extended -not -regex ".*\\.(jar|ear)"

This will do the job, and I personally find it a bit clearer than some of the other solutions. Unfortunately the -regextype is required (cluttering up an otherwise simple command) to make the capturing group work.




回答4:


Using a regular expression in this case sounds like an overkill (you could just check if the name ends with something). I'm not sure about emacs syntax, but something like this should be generic enough to work:

\.(?!((jar$)|(ear$)))

i.e. find a dot (.) not followed by ending ($) "jar" or (|) "ear".



来源:https://stackoverflow.com/questions/6745401/regular-expression-to-exclude-filetypes-from-find

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!