Che*_*bes 5 find regular-expression alpine-linux
这是否与错误等有关,或者事情本来应该如此?
find ./frontend -mindepth 1 -regex '^./dir1/dir2\(/.*\)?'
适用于 Ubuntu,但不适用于 Alpine (docker)
find ./frontend -mindepth 1 -regex '^./dir1/dir2\(/.*\)\?'
适用于 Alpine (docker),但不适用于 Ubuntu
高山:3.14
乌班图:18.04
GNU find 默认-regex
使用Emacs 正则表达式。-regextype
这可以通过特定于 GNU find 的选项进行更改;其他选择包括 POSIX BRE(基本正则表达式,如 grep 和 sed)和 POSIX ERE(扩展正则表达式,如和grep -E
(几乎)awk)。
BusyBox find使用 POSIX BRE(-regex
regexc
函数的默认值)。由于 BusyBox 设计得很小,因此无法选择使用不同的正则表达式语法。
FreeBSD、macOS 和NetBSD默认使用 BRE,并且可以通过该-E
选项使用 ERE。
POSIX没有标准化-regex
。
对于您的命令:
\n\\(\xe2\x80\xa6\\)
. 零或一运算符(\\?
如果存在)是存在的,但它是一个可选功能,在使用 Glibc 构建时存在于 BusyBox 中(我不确定其他 libc),但在 BSD 上则不存在。零或一也可以拼写\\{0,1\\}
。\\(\xe2\x80\xa6\\)
,零或一运算符是?
。尽管 Emacs 本身也支持\\{0,1\\}
表示零或一,但 GNU find 的 Emacs 正则表达式语法却不支持。(\xe2\x80\xa6)
,零或一运算符是?
。find
如果您需要该实现的各种实现之间的可移植性-regex
,则需要坚持使用 POSIX BRE 构造(为了 BusyBox),它们在 GNU find 的 Emacs 语法中拼写相同。这意味着不存在零或一运算符。
find ./frontend -mindepth 1 \\( -regex \'^./dir1/dir2/.*\' -o -regex \'^./dir1/dir2\' \\)\n
Run Code Online (Sandbox Code Playgroud)\n或者,安排传递-regextype posix-basic
给 GNU find。
case $(find --help 2>/dev/null) in\n *-regextype*) find_options=\'-regextype posix-basic\';;\n *) find_options=;;\nesac\nfind ./frontend $find_options -mindepth 1 -regex \'^./dir1/dir2\\(/.*\\)\\{0,1\\}\'\n
Run Code Online (Sandbox Code Playgroud)\n如果dir1
和dir2
是普通字符串而不是正则表达式,那么您将无法使用-regex
,您可以只写
find ./frontend/dir1/dir2 -maxdepth 1\n
Run Code Online (Sandbox Code Playgroud)\n