For example, this doesn't find the file:

: > 12; basename $(find -E . -regex '^(.*)(\d{2})' -print 2>/dev/null)

But this does:

basename $(find -E . -regex '^(.*)([0-9]{2})' -print 2>/dev/null)

At the same time, for example:

echo -e '12\nabc\n456' | grep -E -e '^(\d{3})'

works quite well, though seemingly uses the same regcomp().

What am I doing wrong and how can I fix it?

  • 1
    Newer OS X grep use a BSD code base, not GNU grep. The -E option is a mosh of POSIX ERE and PCRE and I believe exclusive to OS X. find uses BRE and ERE and the \d character class is invalid. – fd0 Sep 29 '16 at 20:40
  • @fd0 Best comment! – cave-canem Oct 3 '16 at 4:00

It appears there is a bug in the native MacOS find in which its -regex option does not correctly handle any of the Shortcuts described in man re_format. The following command seems to work:

find -E . -regex '^(.*)([[:digit:]]{2})' -print

This slightly simpler variant also seems to work:

find -E . -regex '.*[[:digit:]]{2}' -print

In short, grep -E and finds -regex option don't seem to handle regular expressions in exactly the same way.

|improve this answer|||||
  • @Erik Fuller, I think it's about something different, as well as the gfind . -regextype 'egrep' -regex '^(.*)(\d{2})' -print 2>/dev/null does not work – cave-canem Sep 29 '16 at 17:18
  • It's possible gfind has the same bug. In any case there seems to be a viable workaround. – Eirik Fuller Sep 29 '16 at 18:06
  • the problem is that drops out the whole class of "enhanced basic and enhanced extended REs". In this case it is not clear why "egrep" is working properly, because it also uses the same regcomp () – cave-canem Sep 29 '16 at 20:06
  • It drops out some of the extended REs, but not all of them; the [[:digit:]] construct described in this answer does not seem to work in find without -E. – Eirik Fuller Sep 29 '16 at 20:09
  • Not extended REs, but "enhanced basic and enhanced extended REs" – cave-canem Sep 29 '16 at 20:55

ENHANCED FEATURES is not available in the current implementation of find, as in the compiled version of find REG_ENHANCED flag is non passed (See regex(3) and https://opensource.apple.com/source/shell_cmds/shell_cmds-187/find/main.c).

|improve this answer|||||

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .