为什么此正则表达式不起作用:find ./-regex'.* \(m \ | h \)$ [英] why isn't this regex working : find ./ -regex '.*\(m\|h\)$

查看:146
本文介绍了为什么此正则表达式不起作用:find ./-regex'.* \(m \ | h \)$的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why isn't this regex working?

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

我注意到以下工作正常:

I noticed that the following works fine:

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

但是当我通过添加\|h来添加或文件名末尾的h"时,它将无法正常工作.也就是说,它应该拾取我所有的*.m*.h文件,但我什么也没回来.

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.

我在Mac OS X上.

I am on Mac OS X.

推荐答案

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

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

[基本]正则表达式在几个方面有所不同. |是一个普通字符,其功能无与伦比.

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

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

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

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

或者您可以添加-E选项,以告知find使用扩展正则表达式.

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

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

不幸的是,-E无法携带.

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

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.

正则表达式语法摘要 [ 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆