-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

Globs Aren't Regular Expressions

Bash does not do regular expression file matches. Shells generally do pattern matching with "globs." You can turn on extended globs with shopt -s extglob, but you still need to perform globbing on its own terms. For example, this will match your file:

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*)

In this example, the extended globs are not really necessary. You could do the same thing more easily with standard globs. However, they provide some simple examples of what you can do.

于 2012-07-18T02:45:45.913 に答える
1

bash does not support glob expansion via regular expressions. You need to use glob syntax or extglob syntax, neither of which allows {4} for number of occurrences.

Try `ls | pcregrep' or something like that instead, or provide more info on what you are trying to do.

于 2012-07-18T02:43:58.000 に答える
0

Bash expands glob patterns, not regular expressions.

于 2012-07-18T02:43:51.903 に答える