为什么这个正则表达式不起作用:find ./ -regex '.*\(m\|h\)$
为什么这个正则表达式不起作用?
find ./ -regex '.*\(m\|h\)$
我注意到以下工作正常:
find ./ -regex '.*\(m\)$'
但是当我通过添加 \|h
添加“或 ah 在文件名末尾”时,它不起作用。也就是说,它应该拾取我所有的 *.m
和 *.h
文件,但我什么也没得到。
我使用的是 Mac OS X。
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 a h 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.
But when I add the "or a h 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更有效的解决方案是使用
-o
标志:但如果您想要正则表达式,请使用:
A more efficient solution is to use the
-o
flag:but if you want the regex use:
好吧,这有点 hacky,但如果你不想解决 OSX 上 find 的正则表达式限制,你可以将 find 的输出通过管道传输到 grep:
Okay this is a little hacky but if you don't want to wrangle the regex limitations of find on OSX, you can just pipe find's output to grep:
有什么问题。
如果您想要奇特的模式,那么使用 find2perl 并破解该模式
What’s wrong with
If you want fancy patterns, then use find2perl and hack the pattern.
在 Mac OS X 上,您不能在基本正则表达式中使用
\|
,这是find
默认使用的。re_format 手册页
在这种情况下,最简单的解决方法是将
\(m\|h\)
更改为[mh]
,例如或者您可以添加
-E
告诉 find 使用扩展正则表达式的选项。不幸的是
-E
不可移植。另请注意,如果您只想列出以
.m
或.h
结尾的文件,则必须转义点,例如,如果您发现这令人困惑(我也是),有一个很好的参考表,显示了哪些系统支持哪些功能。
正则表达式语法摘要 [Google 缓存]
如果您发现这令人困惑(我也是),有一个很好的参考表,显示了哪些系统支持哪些功能。
正则表达式语法摘要 [Google 缓存]
或者您可以添加
-E
告诉 find 使用扩展正则表达式的选项。不幸的是
-E
不可移植。另请注意,如果您只想列出以
.m
或.h
结尾的文件,则必须转义点,例如,如果您发现这令人困惑(我也是),有一个很好的参考表,显示了哪些系统支持哪些功能。
正则表达式语法摘要 [Google 缓存]
不幸的是
-E
不可移植。另请注意,如果您只想列出以
.m
或.h
结尾的文件,则必须转义点,例如,如果您发现这令人困惑(我也是),有一个很好的参考表,显示了哪些系统支持哪些功能。
正则表达式语法摘要 [Google 缓存]
或者您可以添加
-E
告诉 find 使用扩展正则表达式的选项。不幸的是
-E
不可移植。另请注意,如果您只想列出以
.m
或.h
结尾的文件,则必须转义点,例如,如果您发现这令人困惑(我也是),有一个很好的参考表,显示了哪些系统支持哪些功能。
正则表达式语法摘要 [Google 缓存]
On Mac OS X, you can't use
\|
in a basic regular expression, which is whatfind
uses by default.re_format man page
The easiest fix in this case is to change
\(m\|h\)
to[mh]
, e.g.Or you could add the
-E
option to tell find to use extended regular expressions instead.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, e.g.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]
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]
Or you could add the
-E
option to tell find to use extended regular expressions instead.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, e.g.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]
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, e.g.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]
Or you could add the
-E
option to tell find to use extended regular expressions instead.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, e.g.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]