Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does find -regex not accept my regex?

Tags:

regex

find

I want to select some files that are matching a regular expression. Files are for example:

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

I want all files that match this regex:

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

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

I can't figure it out, I am using:

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
like image 728
Tobias Avatar asked Jun 20 '11 14:06

Tobias


2 Answers

bash4+ and perl

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

you should have in your .profile shopt -s globstar

like image 106
jm666 Avatar answered Sep 18 '22 02:09

jm666


According to the find man page the find regex uses per default emacs regex. And according to http://www.regular-expressions.info/refflavors.html emacs is GNU ERE and that does not support look arounds.

You can try a different -regextype like @l0b0 suggested, but also the Posix flavours seems to not support this feature.

like image 40
stema Avatar answered Sep 18 '22 02:09

stema