-1

我正在尝试使用正则表达式列出文件,但它没有按预期列出。

# ls test*.tgz
test-2012_07_17_11_23_45.tgz

但是当我使用一点钻的正则表达式时,它就失败了。

# ls test-[0-9]{4}_*.tgz
ls: cannot access test-[0-9]{4}_*.tgz: No such file or directory

# ls test-[0-9]\{4\}_*.tgz
ls: cannot access test-[0-9]{4}_*.tgz: No such file or directory

# ls "test-"[0-9]\{4\}"_"*".tgz"
ls: cannot access test-[0-9]{4}_*.tgz: No such file or directory

任何指针都会有所帮助。

4

3 回答 3

3

Glob 不是正则表达式

Bash 不进行正则表达式文件匹配。Shell 通常使用“globs”进行模式匹配。您可以使用 来打开扩展的shopt -s extglobglob,但您仍然需要按照自己的条件执行 glob。例如,这将匹配您的文件:

shopt -s extglob

# Would match: test-2011_07_17_11_23_45.tgz test-2012_07_17_11_23_45.tgz
ls test-+(+([[:digit:]])_*tgz)

# Would match: test-2011_07_17_11_23_45.tgz
ls test-!(2012*)

在此示例中,扩展的 glob 并不是真正需要的。您可以使用标准 glob 更轻松地完成相同的操作。但是,它们提供了一些您可以做什么的简单示例。

于 2012-07-18T02:45:45.913 回答
1

bash 不支持通过正则表达式进行 glob 扩展。您需要使用 glob 语法或 extglob 语法,它们都不允许 {4} 出现次数。

试试`ls | pcregrep' 或类似的东西,或者提供更多关于你想要做什么的信息。

于 2012-07-18T02:43:58.000 回答
0

Bash 扩展 glob 模式,而不是正则表达式。

于 2012-07-18T02:43:51.903 回答