为什么这个正则表达式不起作用:find ./ -regex '.*\(m\|h\)$

发布于 2024-11-05 02:40:23 字数 297 浏览 0 评论 0原文

为什么这个正则表达式不起作用?

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神经暖 2024-11-12 02:40:24

更有效的解决方案是使用 -o 标志:

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

但如果您想要正则表达式,请使用:

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

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]$"
╰つ倒转 2024-11-12 02:40:24

好吧,这有点 hacky,但如果你不想解决 OSX 上 find 的正则表达式限制,你可以将 find 的输出通过管道传输到 grep:

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

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:

find . | grep ".*\(\h\|m\)"
童话 2024-11-12 02:40:24

有什么问题。

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

如果您想要奇特的模式,那么使用 find2perl 并破解该模式

What’s wrong with

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

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

铁轨上的流浪者 2024-11-12 02:40:23

在 Mac OS X 上,您不能在基本正则表达式中使用 \|,这是 find 默认使用的。

re_format 手册页

[基本]正则表达式在几个方面有所不同。 |是一个普通字符,没有与其功能相当的字符。

在这种情况下,最简单的解决方法是将 \(m\|h\) 更改为 [mh],例如

find ./ -regex '.*[mh]

或者您可以添加 -E 告诉 find 使用扩展正则表达式的选项。

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

不幸的是 -E 不可移植。

另请注意,如果您只想列出以 .m.h 结尾的文件,则必须转义点,例如,

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

如果您发现这令人困惑(我也是),有一个很好的参考表,显示了哪些系统支持哪些功能。

正则表达式语法摘要 [Google 缓存]

或者您可以添加 -E 告诉 find 使用扩展正则表达式的选项。


不幸的是 -E 不可移植。

另请注意,如果您只想列出以 .m.h 结尾的文件,则必须转义点,例如,


如果您发现这令人困惑(我也是),有一个很好的参考表,显示了哪些系统支持哪些功能。

正则表达式语法摘要 [Google 缓存]

不幸的是 -E 不可移植。

另请注意,如果您只想列出以 .m.h 结尾的文件,则必须转义点,例如,


如果您发现这令人困惑(我也是),有一个很好的参考表,显示了哪些系统支持哪些功能。

正则表达式语法摘要 [Google 缓存]

或者您可以添加 -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 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], e.g.

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, e.g.

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]

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]

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文