Why doesn't find -regex accept my regex?

I want to select some files matching the regular expression. Files, for example:

4510-88aid-50048-INA.txt 4510-88nid-50048-INA.txt xxxx-05xxx-xxxxx-INA.txt 

I want all files to match this regex:

 .*[\w]{4}-05(?!aid)[\w]{3}-[\w]{5}-INA\.txt 

In my opinion, this should be xxxx-05xxx-xxxxx-INA.txt in the case above. Using some tool like RegexTester , everything works fine. Using the bash find -regex doesn't seem to work for me. My question is why?

I can not figure it out, I use:

 find /some/path -regex ".*[\w]{4}-05(?!aid)[\w]{3}-[\w]{5}-INA\.txt" -exec echo {} \; 

But nothing is printed ... Any ideas?

 $ uname -a Linux debmu838 2.6.5-7.321-smp #1 SMP Mon Nov 9 14:29:56 UTC 2009 x86_64 x86_64 x86_64 GNU/Linux 
+4
source share
6 answers

bash4 + and perl

 ls /some/path/**/*.txt | perl -nle 'print if /^[\w]{4}-05(?!aid)[\w]{3}-[\w]{5}-INA\.txt/' 

you must have in your .profile shopt -s globstar

+5
source

According to the search page, the search term regex uses emacs regex by default. And according to http://www.regular-expressions.info/refflavors.html emacs is GNU ERE and it does not support appearance.

You can try another -regextype like @ l0b0, but also Posix flavors don't seem to support this feature.

+4
source

You have a Perl regex. Try with another -regextype and adjust the -regextype accordingly:

Changes the syntax of a regular expression is understood as the -regex and -iregex tests, which appear later in the line command. The types emacs (this is the default), posix-awk, posix-basic, posix-egrep, and posix-extended are currently implemented.

+2
source

I pretty much repeat the other answers: Find -regex switch cannot emulate everything in a Perl regex, however here you can try ...

Take a look at the find2perl command. This program can take a typical find statement and provide you with the equivalent Perl program. I do not believe that -regex recognized by find2perl (it is not part of the standard Unix search, but only in the GNU search), but you can just use -name and then look at the program it creates. From there, you can modify the program to use the Perl expressions you want in your regular expression. As a result, you will get a small Perl script that will make the search for the file directory necessary.

Otherwise, try using -regextype posix-extended , which pretty much matches most Perl regular expression expressions. You cannot use the look, but you can probably find something that works.

+1
source

Try the following:

 ls ????-??aid-?????-INA.txt 
0
source

Try a simple script as follows:

 #!/bin/bash for file in *INA.txt do match=$(echo "${file%INA.txt}" | sed -r 's/^\w{4}-\w{5}-\w{5}-$/found/') [ $match == "found" ] && echo "$file" done 
0
source

All Articles