简体   繁体   中英

why isn't this regex working : find ./ -regex '.*\(m\|h\)$

Why isn't this regex working?

  find ./ -regex '.*\(m\|h\)$

I noticed that the following works fine:

  find ./ -regex '.*\(m\)$'

But when I add the "or ah at the end of the filename" by adding \\|h it doesn't work. That is, it should pick up all my *.m and *.h files, but I am getting nothing back.

I am on Mac OS X.

On Mac OS X, you can't use \\| in a basic regular expression, which is what find uses by default.

re_format man page

[basic] regular expressions differ in several respects. | is an ordinary character and there is no equivalent for its functionality.

The easiest fix in this case is to change \\(m\\|h\\) to [mh] , eg

find ./ -regex '.*[mh]$'

Or you could add the -E option to tell find to use extended regular expressions instead.

find -E ./ -regex '.*(m|h)$'

Unfortunately -E isn't portable.

Also note that if you only want to list files ending in .m or .h , you have to escape the dot, eg

find ./ -regex '.*\.[mh]$'

If you find this confusing (me too), there's a great reference table that shows which features are supported on which systems.

Regex Syntax Summary [ Google Cache ]

A more efficient solution is to use the -o flag:

find . -type f \( -name "*.m" -o -name "*.h" \)

but if you want the regex use:

find . -type f -regex ".*\.[mh]$"

好吧这有点hacky但是如果你不想在OSX上争论find的正则表达式限制,你可以将find的输出管道输出到grep:

find . | grep ".*\(\h\|m\)"

What's wrong with

find . -name '*.[mh]' -type f

If you want fancy patterns, then use find2perl and hack the pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM