在“查找”中工作的正则表达式

com*_*nda 2 regex unix bsd find

我有一个目录,其中包含约 8000 个以下形式的文件

output/Manuscript_00750_AnimalGiants-compact.json
output/Manuscript_00750_AnimalGiants-expanded.json
output/Manuscript_00750_AnimalGiants.json
output/Manuscript_00752_AnimalGiants-compact.json
output/Manuscript_00752_AnimalGiants-expanded.json
output/Manuscript_00752_AnimalGiants.json
output/Unit_TZH_12345_Foo-compact.json
output/Unit_TZH_12345_Foo-expanded.json
output/Unit_TZH_12345_Foo.json
Run Code Online (Sandbox Code Playgroud)

我需要想出一个正则表达式来使用该find工具来仅选择 Manuscript-compact 的:

output/Manuscript_00750_AnimalGiants-compact.json
output/Manuscript_00752_AnimalGiants-compact.json
Run Code Online (Sandbox Code Playgroud)

提出正则表达式是容易的部分,但进行find合作是困难的部分。

这是我的正则表达式:

/Manuscript[0-9_a-zA-Z]+-compact\.json/
Run Code Online (Sandbox Code Playgroud)

以下是我尝试过的一些命令;全部产生零结果。cwd 是上面的目录output/

find output -regex "Manuscript[0-9_a-zA-Z]+-compact\.json"
find output -regex "\./output/Manuscript[0-9_a-zA-Z]+-compact\.json/"
find output -regex ".*\Manuscript[0-9_a-zA-Z]+-compact.*\json"
Run Code Online (Sandbox Code Playgroud)

但这个命令确实产生了结果 - 它选择了所有以“Manuscript”开头的文件,这显然太宽泛了:

find output -regex ".*\Manuscript.*\json"
Run Code Online (Sandbox Code Playgroud)

这里正确的正则表达式格式是什么find

anu*_*ava 5

在 OSX 上,您可以将其find与扩展正则表达式一起使用:

find -E output -regex '.*/Manuscript[0-9_a-zA-Z]+-compact\.json$'
Run Code Online (Sandbox Code Playgroud)

在 gnu 上find使用:

find output -regextype posix-extended -regex '.*/Manuscript[0-9_a-zA-Z]+-compact\.json$'
Run Code Online (Sandbox Code Playgroud)