Pasha Pasha - 5 months ago 31
Linux Question

regex to exclude words for find tool

I try to find files with

util and I want to find all
-files except files in the special directories. I try to find with command:

find ./ -regex '\.((?\!node_modules).)*\.js'

But this scenario finds nothing.

This regex works for Sublime Text 3, where I've tested it.

P.S. I know about solving with another options like below. But I want to know how to solve the problem with regex

find ./ -path *node_modules -prune -o -name *.js


You can't do it at the moment.

GNU Find supports several regex types, but none of them supports zero width assertions.

-regextype type
          Changes  the regular expression syntax understood by -regex and -iregex tests which occur later on the command line.  Currently-implemented types are emacs (this is the default), > posix-awk, posix-basic, posix-
          egrep and posix-extended.

For your task you need the lookbehind assertion, and it is not supported yet.

Another option for you is to use find2perl that converts a find expression to a perl program, that makes the same what the expression makes.

You could use this program to check if your expression will work with PCRE and look-ahead/look-behind assertions.