正規表現の検索では中かっこ式が機能しません。

正規表現の検索では中かっこ式が機能しません。

現在のフォルダCA01の下に2つのフォルダがあります。CA02foo

foo -+
     |
     +-CA01
     |
     +-CA02

私が入力している間

find . -regex ".*CA[0-9]+" -exec echo {} +

または

find . -regex ".*CA[0-9][0-9]" -exec echo {} +

予想される結果は次のとおりです。

./CA01 ./CA02

しかし、私が入力したとき

find . -regex ".*CA[0-9]\{2\}" -exec echo {} +

何も現れなかったが、本当に予想外のことだった。

emacsはデフォルトでfind正規表現を使用するためです。上記のすべての項目を使用して、両方のフォルダを一致させることができます。

ここで何か抜けましたか?

ベストアンサー1

-regextype重複計算をサポートするように変更する必要があります(例{2}:)。基本はemacs計算をサポートしていないようです。デフォルトの正規表現型は、反復計算構文を持たない以前のバージョンのEmacsを模倣します。次のタイプが私に適しているようです。

はい

posix-egrep

$ find foo -regextype posix-egrep -regex ".*CA[0-9]{2}" -exec echo {} +
foo/CA02 foo/CA01

sed

$ find foo -regextype sed -regex ".*CA[0-9]\{2\}" -exec echo {} +
foo/CA02 foo/CA01

POSIX拡張

$ find foo -regextype posix-extended -regex ".*CA[0-9]{2}" -exec echo {} +
foo/CA02 foo/CA01

他にもありましたが、もう一度試してみませんでした。findマニュアルページを参照してを検索してください-regextype

抜粋

-regextype type
       Changes  the  regular  expression  syntax  understood by -regex and 
       -iregex tests which occur later on the command line.  Currently
       implemented types are emacs (this is the default), posix-awk, 
       posix-basic, posix-egrep and posix-extended.

私のバージョンを探す

$ find -version
find (GNU findutils) 4.5.9
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version 1778ee9e7d0e150a37db66a0e51c1a56755aab4f
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2) 

おすすめ記事