如何构造 [0-9a-f] 类型的恰好 38 个字符的正则表达式?
How to construct regex of exactly 38 chars of type [0-9a-f]?
我正在执行文件查找操作,我想排除所有 GIT 哈希文件,这些文件的名称是 38 个十六进制数。
不过我觉得写起来不太优雅
find . -not -name "[0-9a-f]" <-- 38 times.
还有其他方法吗?
使用括号:
find . -not -regex ".*[0-9a-f]\{38\}"
Escaped "curly brackets" -- { } -- indicate the number of occurrences of a >preceding RE to match.
It is necessary to escape the curly brackets since they have only their literal >character meaning otherwise. This usage is technically not part of the basic RE >set.
"[0-9]{5}" matches exactly five digits (characters in the range of 0 to 9).
http://tldp.org/LDP/abs/html/x17129.html
注意:之前的回答(find . -not -name "[0-9a-f]\{38\}"
)不正确;正如@Cyrus 所指出的,该 -regex 选项是必需的。此外,您必须使用 .* 作为前缀(或类似的东西)以允许 find 匹配目录(也许这很清楚,但在我测试解决方案时它绊倒了我)。
小例子:
ls -C1
ab
abab
abcbade
ac
acba
acca
adda
d
find . -regex ".*[a-c]\{4\}"
./abab
./acba
./acca
find . -not -regex ".*[a-c]\{4\}"
.
./ab
./abcbade
./ac
./adda
./d
我正在执行文件查找操作,我想排除所有 GIT 哈希文件,这些文件的名称是 38 个十六进制数。
不过我觉得写起来不太优雅
find . -not -name "[0-9a-f]" <-- 38 times.
还有其他方法吗?
使用括号:
find . -not -regex ".*[0-9a-f]\{38\}"
Escaped "curly brackets" -- { } -- indicate the number of occurrences of a >preceding RE to match.
It is necessary to escape the curly brackets since they have only their literal >character meaning otherwise. This usage is technically not part of the basic RE >set.
"[0-9]{5}" matches exactly five digits (characters in the range of 0 to 9).
http://tldp.org/LDP/abs/html/x17129.html
注意:之前的回答(find . -not -name "[0-9a-f]\{38\}"
)不正确;正如@Cyrus 所指出的,该 -regex 选项是必需的。此外,您必须使用 .* 作为前缀(或类似的东西)以允许 find 匹配目录(也许这很清楚,但在我测试解决方案时它绊倒了我)。
小例子:
ls -C1
ab
abab
abcbade
ac
acba
acca
adda
d
find . -regex ".*[a-c]\{4\}"
./abab
./acba
./acca
find . -not -regex ".*[a-c]\{4\}"
.
./ab
./abcbade
./ac
./adda
./d