Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Unix find命令中数字的正则表达式_Regex_Unix_Command Line_Find - Fatal编程技术网

Regex Unix find命令中数字的正则表达式

Regex Unix find命令中数字的正则表达式,regex,unix,command-line,find,Regex,Unix,Command Line,Find,我有这个命令: find reports/ -type f -mtime +90 -regex ".*\.\(csv\|sql\|txt\|xls\|zip\)" 我需要加强它,使文件扩展名之前的部分匹配YYYY/MM/DD模式,如下所示: reports/2010/10/10/23.txt reports/2010/10/10/23.xls reports/2010/10/10/26.csv reports/2010/10/10/26.sql reports/2010/10/10/26.tx

我有这个命令:

find reports/ -type f -mtime +90 -regex ".*\.\(csv\|sql\|txt\|xls\|zip\)"
我需要加强它,使文件扩展名之前的部分匹配
YYYY/MM/DD
模式,如下所示:

reports/2010/10/10/23.txt
reports/2010/10/10/23.xls
reports/2010/10/10/26.csv
reports/2010/10/10/26.sql
reports/2010/10/10/26.txt
reports/2010/10/10/26.xls
reports/2010/10/10/27.csv
但是我没有得到任何排列的
\d
和帕伦斯逃跑去工作

更新:根据以下公认的答案,以下是对我有效的方法:

find reports/ -type f -mtime +90 -regex "reports/201[01]/\([1-9]\|1[012]\)/\([1-9]\|[12][0-9]\|3[01]\)/.*\.\(csv\|sql\|txt\|xls\|zip\)"

\d
是Emacs正则表达式和POSIX正则表达式不支持的正则表达式的扩展(这些是
find
支持的风格)。您可以使用
[[:digit:][]
[0-9]
来代替。

以下内容很难看,无法删除无效日期,但可能已经足够接近了:

find reports/ -type f -regex ".*/reports/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/[0-9][0-9]\.\(csv\|sql\|txt\|xls\|zip\)"

这是我过去使用过的:

Year: (19|20)[0-9][0-9]
Month:  0[1-9]|1[012]
Day: (0[1-9]|[12][0-9]|3[01])

你可以把这些放在你的正则表达式中。当然,您必须避开支架和管道。

您可以这样使用中继器:

find ./ -regextype posix-egrep -iregex ".*\._[0-9]{8}-[0-9]{6}.*"
我使用它查找表单的备份:

./foo._20140716-121745.OLD
其中
foo
是原始名称,数字是日期和时间

(在CentOS 6.5上)


另外,regextype posix extended也可以使用。

注意,-regex选项在Unix平台上不是标准选项。我想可能只有在GNU上才能找到。您可以通过将输出管道化到egrep来实现更标准的方法。这看起来不错(我稍后会对其进行测试),但是否可以使用类似“[0-9]{4}”的内容来压缩范围,而不是连续重复四次?数字量词“{4}”在我的系统(libc 2.3.4)上由
find
使用的
libc
版本中,似乎无法使用
regexec
版本。YMMV.您可以使用
[0-9]
,但是否可以使用
[[:digit:]
取决于您使用的
-regextype
。例如,
emacs
(默认类型)不支持它,而
posix-extended
支持它。有关底部链接的语法说明,请参见。